大佬们,我初学Spring AOP,碰到了切面类的通知方法不启动的问题,想求大佬深入讲解一下原因,具体的问题有:
-
在PointCut中设置了目标类中所有的方法,构造方法为什么不会启动通知?
-
直接调用目标方法,为什么不会启动通知?
1. 首先我快速搭了一个springboot web项目,确定可以正常运行,然后创建了一个controller类
@RestController
public class HelloWorld {
private String words;
HelloWorld(){
this.words = "HelloWorld!";
this.say();
}
@RequestMapping("/hello")
public void say(){
System.out.println(this.words);
GoodBye goodBye = new GoodBye();
goodBye.say();
}
}
2. 然后我创建了一个切面类
@Aspect
@Component
public class FirstAOP {
@Pointcut(value = "execution(* com.springaop.HelloWorld.*(..))")
public void logAOP(){
}
@Around(value = "logAOP()")
public Object doAround(@NotNull ProceedingJoinPoint pjp) throws Throwable {
System.out.println("around");
return pjp.proceed();
}
@Before(value = "logAOP()")
public void doBefore(){
System.out.println("before");
}
@After(value = "logAOP()")
public void doAfter(){
System.out.println("after");
}
}
3. 我在main方法中调用HelloWorld中的say()
public static void main(String[] args) {
SpringApplication.run(SpringAopApplication.class, args);
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
annotationConfigApplicationContext.register(HelloWorld .class);
annotationConfigApplicationContext.refresh();
HelloWorld hello= annotationConfigApplicationContext.getBean(HelloWorld .class);
hello.say();
}
4. 结果
最后控制台只是输出了 HelloWorld!
,在网页端输入 localhost:8080/hello
地址以后控制台再次输出,这时才有AOP通知的内容
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.6)
2022-04-20 15:56:09.929 INFO 16216 --- [ main] com.springaop.SpringAopApplication : Starting SpringAopApplication using Java 1.8.0_271
2022-04-20 15:56:09.940 INFO 16216 --- [ main] com.springaop.SpringAopApplication : No active profile set, falling back to 1 default profile: "default"
2022-04-20 15:56:10.718 INFO 16216 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-04-20 15:56:10.726 INFO 16216 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-04-20 15:56:10.726 INFO 16216 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.60]
2022-04-20 15:56:10.887 INFO 16216 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-04-20 15:56:10.887 INFO 16216 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 913 ms
HelloWorld!
2022-04-20 15:56:11.318 INFO 16216 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-04-20 15:56:11.326 INFO 16216 --- [ main] com.springaop.SpringAopApplication : Started SpringAopApplication in 1.751 seconds (JVM running for 2.642)
2022-04-20 15:56:13.637 INFO 16216 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-04-20 15:56:13.637 INFO 16216 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-04-20 15:56:13.638 INFO 16216 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
around
before
HelloWorld!
after
Process finished with exit code 130
求大佬解答!