Spring AOP Advice on Annotated Controllers
Spring AOP (Aspect-oriented programming) framework is used to modularize cross-cutting concerns in aspects. If you want a more simple definition you can think of them as a Interceptor but with more options configurations possible. In Spring there are two different constructs that get called “interceptors”. First, there are Handler Interceptors, which are part of the Spring MVC framework (and similar to Interceptors in Struts 2), and give you the ability to add interceptor logic to requests. But you also have Method Interceptors, which are part of the Spring AOP framework. These are much more general mechanism than Handler Interceptors, but also potentially more complex. In AOP terminology, such interceptors provide a means of coding the “aspects” you’re talking about.
In this article we are not going to cover how Spring AOP work, I am going to present the solution to an error that could appear when you want to use advices with controller methods in Spring 3 using annotation and non declarative configurations.
¿What is the problem?
You need to make some processing (before and/or after) the execution of a controller. You want to use the AspectJ annotation in Spring to do that. Your code could be something like:
The aspect has to include an advice which will be executed before and after the controller is invoked by a request. Using annotation your aspect could be something similar to:
So you want to advice all the methods that contains getViewNew in the name from all classes annotated with @Controller. When you execute this code deploying the app in a server it doesn’t work and don’t raise any error or exception.
The cause is…
After debugging the code I realized that the advice is never been executed. So I tried to look for a solution in StackOverflow. Some of the question/answer that I find there were:
http://stackoverflow.com/questions/3310115/spring-aop-advice-on-annotated-controllers
http://stackoverflow.com/questions/789759/how-can-i-apply-an-aspect-using-annotations-in-spring
http://stackoverflow.com/questions/9310927/aspect-not-executed-in-spring
But anyone of these discussion and the solutions that are proposed in, works in my situation. The question that point me in the right direction was:
http://stackoverflow.com/questions/3991249/how-can-i-combine-aspect-with-controller-in-spring-3
The source of the problem is: Spring AOP is based on a proxy generation, that executed the advices in the pointcuts that you declare.For accomplish this, the classes that contain the methods that match with the pointcuts which you created (the classes we want to intercept the execution) have to be declared with component scanning in the domain on the DispatcherServlet.
At this point, if your using a xml file to describe the dispatched servlet, like
for the web.xml file, and:
But, what happen if you don’t have a dispatcher servlet XML file and you are using a class that extends WebMvcConfigurerAdapter to create the servlet configuration. In this case the advice is never executed because the controllers are not created in the domain of the DispatcherServlet.
The solution is…
There are two possible solutions to this problem:
- Change the configuration of your spring application to use a xml configuration instead of the WebMvcConfigurerAdapter.
- Create a aop xml file to declare the class that implement the advice and to configure the aop proxy to make use of this advice.
The disadvantage of this solution is: each time you have to create a new advice, you have to remember change the xml file to declare the advice and add the advice to the proxy.
——————————————–
References:
Spring AOP Reference Documentation.
Article in Java Geek Codes: a quick tutorial with full code.
http://stackoverflow.com/questions/3310115/spring-aop-advice-on-annotated-controllers
http://stackoverflow.com/questions/789759/how-can-i-apply-an-aspect-using-annotations-in-spring
http://stackoverflow.com/questions/9310927/aspect-not-executed-in-spring
http://stackoverflow.com/questions/3991249/how-can-i-combine-aspect-with-controller-in-spring-3
Category: Development, Java
相关推荐
Spring Mvc AOP通过注解方式拦截controller等实现日志管理
基于注解配置和使用spring AOP(spring mvc框架)
演示了spring对aop的支持,包括注解方式、基于xml方式。
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
Spring MVC AOP通过注解方式拦截Controller等实现日志管理demo版本2
Spring mvc mybatis plus 实现AOP 切面日志系统,带有数据库。可以自行拓展
Spring MVC AOP通过自定义注解方式拦截Controller等实现日志管理, springMVC里做添加AOP拦截,用于捕获异常。
有人问 Sping AOP用AspectJ注解的方式拦截不到SpringMVC的controller方法? 我这里提供了一种解决方法,仅供参考
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
源代码是spring-boot添加aop切面功能的demo,简单易懂.
开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...
找了很久,也没在网上找到spring mvc框架下的aop例子~研究了好久终于写出来了~拿出来和大家分享一下~
spring mvc aop <context:annotation-config />
aopalliance-1.0.jar,org.springframework.aop-3.0.0.RELEASE.jar,org.springframework.jdbc-3.0.0.RELEASEorg.springframework.beans-3.0.0.RELEASE.jar等
使用面向切面的编程,构建一个日志记录的切面 a. 对于所有的Alumni表的查询操作,记录各个操作的时间、用户,读取内容,存入ReadLog表格中。 b. 对于所有的Alumni表的更新(更新和删除)操作,记录各个操作的时间、...
基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)...
Spring IOC AOP MVC 简单例子
├── spring-aop-4.3.0.RELEASE.jar ├── spring-aspects-4.3.0.RELEASE.jar ├── spring-beans-4.3.0.RELEASE.jar ├── spring-context-4.3.0.RELEASE.jar ├── spring-context-support-4.3.0....
springaop拦截controller日志
springIOC与AOP配置