I am working on a spring boot application and I am trying to implement a custom annotation to log execution time and memory used by the method annotated with my custom annotation. I am not able to make it work and have no clue why its not working. Я нашел похожие сообщения и различные примеры, но ни один из них не работает в моем случае.
Пожалуйста, дайте мне знать, если я делаю что-то не так.
< p>Подробности моего кода приведены ниже:
зависимости build.gradle:
Код: Выделить всё
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
implementation "org.springframework.boot:spring-boot-starter-oauth2-resource-server"
implementation 'io.pivotal.spring.cloud:spring-cloud-services-starter-service-registry'
implementation 'io.pivotal.spring.cloud:spring-cloud-services-starter-config-client'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation 'com.google.cloud:spring-cloud-gcp-starter'
implementation 'com.google.cloud:spring-cloud-gcp-starter-pubsub'
implementation 'com.google.cloud:spring-cloud-gcp-starter-storage'
implementation 'org.springframework.integration:spring-integration-core'
implementation 'javax.xml.bind:jaxb-api:2.3.0'
implementation 'com.sun.xml.bind:jaxb-core:2.3.0'
implementation 'com.sun.xml.bind:jaxb-impl:2.3.0'
implementation 'javax.annotation:javax.annotation-api:1.3.2'
implementation 'org.springdoc:springdoc-openapi-ui:1.4.3'
implementation 'io.swagger:swagger-annotations:1.6.1'
implementation 'javax.validation:validation-api:2.0.1.Final'
implementation 'org.openapitools:jackson-databind-nullable:0.2.1'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.cloud:spring-cloud-starter-loadbalancer'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
implementation('com.oracle.database.jdbc:ojdbc8')
implementation 'org.dom4j:dom4j:2.1.0'
implementation 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.4'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-amqp'
//testImplementation 'io.projectreactor:reactor-test'
testImplementation 'org.springframework.amqp:spring-rabbit-test'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.aspectj:aspectjweaver'
Custom annotation interface:
Код: Выделить всё
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogExecutionProfile {}
Код: Выделить всё
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Aspect
@Component
public class LoggingAspect {
@Around("@annotation(LogExecutionProfile) || execution(public * @LogExecutionProfile *.*(..)))")
public Object profileMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
String className = methodSignature.getDeclaringType().getSimpleName();
String methodName = methodSignature.getName();
long startTime = System.currentTimeMillis();
long memoryAtStart = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
Object result = proceedingJoinPoint.proceed();
long endTime = System.currentTimeMillis();
long memoryAtEnd = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
log.debug("execution time: " + (endTime - startTime) + " ms"
+ " memory used: " + (memoryAtEnd - memoryAtStart) + " байт памяти");
вернуть результат;
}
Код: Выделить всё
@Service
public class MyService {
@LogExecutionProfile
public void someServiceMethod(){
log.debug("service log");
// service method logic
}
}
Код: Выделить всё
service log
execution time: XX ms memory used: XX bytes of memory
Код: Выделить всё
service log
Example 1 : Not Working
Код: Выделить всё
@Pointcut("execution(@com.example.LogExecutionProfile * *.*(..))")
public void annotatedMethod() {}
@Around("annotatedMethod()")
public Object profileMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
**
}
Код: Выделить всё
@Pointcut("@annotation(com.example.LogExecutionProfile)")
public void annotatedMethod() {}
@Around("execution(* *(..)) && (annotatedMethod())")
public Object profileMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
**
}
Код: Выделить всё
@Around("@annotation(com.example.LogExecutionProfile)")
public Object profileMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
**
}
Источник: https://stackoverflow.com/questions/781 ... ing-aspect