[英]Logger AOP - intercept all calls to the public methods
I need to solve this:我需要解决这个问题:
In the LoggerAOP class, using Spring AOP, intercept all calls to the public methods annotated with the LogExecution annotation, and call the log method on the logger field with the intercepted method's name as the data argument.在 LoggerAOP class 中,使用 Spring AOP,拦截对带有 LogExecution 注解的公共方法的所有调用,并在 logger 字段上调用 log 方法,并将拦截的方法名称作为数据参数。
import org.aspectj.lang.*;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.*;
import java.lang.annotation.*;
import java.util.*;
@Aspect
@Component
public class LoggerAOP {
@Autowired private Logger logger;
public void loggingAdvice(JoinPoint jp) {
}
public static void main(String[] args) {
AnnotationConfigApplicationContext config = new AnnotationConfigApplicationContext();
config.register(Config.class);
config.refresh();
NameRepository repository = config.getBean(NameRepository.class);
System.out.println(repository.getNames());
}
}
@Component
class NameRepository {
@LogExecution
public List<String> getNames() {
List<String> names = new ArrayList<>();
names.add("John");
names.add("Mary");
return names;
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface LogExecution {}
interface Logger {
public void log(String data);
}
@Configuration
@EnableAspectJAutoProxy
@Import({LoggerAOP.class, NameRepository.class})
class Config {
@Bean
public Logger logger() {
return (message) -> System.out.println(message);
}
}
I'm new to Spring so I still have to learn a few things.我是 Spring 的新手,所以我仍然需要学习一些东西。
The code shared with the question works as a spring application without any issues.与问题共享的代码可用作 spring 应用程序,没有任何问题。 Based on the code shared, following aspect would advice all the public methods of beans annotated with
@LogExecution
within package rg.so
and its subpackages.根据共享的代码,以下方面将建议在 package
rg.so
及其子包中使用@LogExecution
注释的 bean 的所有公共方法。 The code uses @Around
advice.该代码使用
@Around
建议。
@Around("@annotation(logExecution) && within(rg.so..*)")
public Object loggingAdvice(ProceedingJoinPoint pjp, LogExecution logExecution) {
logger.log("Before method call :"+pjp.getSignature());
Object obj = null;
try {
obj = pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
logger.log("On error during method call :"+ pjp.getSignature());
}
logger.log("After method call :"+pjp.getSignature());
return obj;
}
Official documentation to refer官方文档参考
Spring AOP: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop Spring AOP: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop
Around advice: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-ataspectj-around-advice周围建议: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-ataspectj-around-advice
Writing good pointcuts: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#writing-good-pointcuts编写好的切入点: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#writing-good-pointcuts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.