我正在尝试练习 Spring AOP。问题是,我的程序不是我所期望的。
Aspect 工作时应该看起来像这样。
Sep 27, 2020 1:11:11 PM aspects.LoggingAspect log
INFO: Method will execute
Sep 27, 2020 1:11:11 PM com.cms.services.CommentService publishComment
INFO: Publishing comment:Demo comment
Sep 27, 2020 1:11:11 PM aspects.LoggingAspect log
INFO: Method executed
但是,它不会记录。就像方面根本不起作用。
com.cms.services.CommentService publishComment
INFO: Publishing Comment: Demo Comment
Main.java使用Instance spring Spring Context执行整体代码 并且还使用 CommentService.java.
CommentService.java只不过是打印一行日志。 它位于 services 包下。
然后是LoggingAspect.java用于切面编程。
最后,CommentConfig.java用于配置EnableAspectJAutoProxy并为Aspect类注册Bean。
Main.java
public class Main {
public static void main(String[] args) {
var c = new AnnotationConfigApplicationContext(CommentConfig.class);
var service = c.getBean(CommentService.class);
Comment comment = new Comment();
comment.setText("Demo Comment");
comment.setAuthor("Natasha");
service.publishComment(comment);
}
}
CommentService.java
@Service
public class CommentService {
private Logger logger = Logger.getLogger(CommentService.class.getName());
public void publishComment(Comment comment) {
logger.info("Publishing Comment: " + comment.getText());
}
}
LoggingAspect.java
@Aspect
@Component
public class LoggingAspect {
private Logger logger = Logger.getLogger(LoggingAspect.class.getName());
@Around("execution(* services.*.*(..))")
public void log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("method will execute");
logger.info("Method will execute");
joinPoint.proceed();
logger.info("Method has executed");
System.out.println("method has executed");
}
}
CommentConfig.java
@Configuration
@ComponentScan(basePackages = {"com.cms"})
@EnableAspectJAutoProxy
public class CommentConfig {
@Bean
public LoggingAspect aspect() {
return new LoggingAspect();
}
}
另外,这是我的依赖。
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.24</version>
</dependency>
无法弄清楚缺少什么...
尝试对 LoggingAspect
进行简单修改@Around("within(*..services..*)")
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("method will execute");
logger.info("Method will execute");
Object proceed = joinPoint.proceed();
logger.info("Method has executed");
System.out.println("method has executed");
return proceed;
}