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方法的透明日志管理,不仅记录正常流程,也能捕获和记录异常,提升系统的可维护性和问题排查效率。 在实际项目中,我们可以根据需求进一步定制日志...
11. **AOP(面向切面编程)**:Spring MVC可以利用Spring的AOP支持实现日志记录、事务管理等跨切面的关注点。 12. **验证(Validation)**:Spring MVC可以配合JSR 303/349的Bean Validation进行数据校验,使用`@...
首先,Spring MVC是Spring框架的一个模块,它专门用于构建Web应用程序的Model-View-Controller(MVC)架构。Spring MVC通过解耦业务逻辑、数据模型和用户界面,提供了灵活的控制层设计。它的主要功能包括请求映射、...
- Spring的JAR包:`spring-context`、`spring-beans`、`spring-aop`,用于Spring的依赖注入和AOP支持。 - Spring MVC的JAR包:`spring-webmvc`,提供Web MVC功能。 - 可能还需要`slf4j-api`和相应的日志实现库,...
2. **Spring MVC**:Spring的Model-View-Controller(MVC)架构为Web应用提供了强大的支持,包括控制器、模型对象、视图解析等,简化了Web应用的开发。 3. **AOP**:Spring的AOP模块支持声明式事务管理、日志记录、...
2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. 动态语言支持 2.6.2. JMX 2.6 .3. 任务规划 2.6.4. 对Java 5(Tiger)的支持 2.7. 移植到Spring 2.0 ...
原理:AOP 是面向切面编程,是通过动态代理的方式为程序添加统一功能,集中解决一些公共问题。好处:1.各个步骤之间的良好隔离性耦合性大大降低、2.源代码无关性,再扩展功能的同时不对源码进行修改操作。 2. ...
SpringMVC 是Spring框架的一部分,是一个用于构建Web应用的MVC(Model-View-Controller)架构。 在"BlazeDS+Spring+SpringMVC 注解方式配置文件"的场景下,我们将关注如何使用注解来简化这三者之间的整合。注解是...
2. **Spring MVC**:作为Spring框架的一部分,Spring MVC是一个用于构建Web应用程序的模型-视图-控制器(Model-View-Controller, MVC)架构。它处理HTTP请求,将请求转发给相应的处理器,然后将处理结果返回给客户端...
它们各自专注于应用程序的不同层次:Struts在表现层提供MVC(Model-View-Controller)架构,Hibernate处理持久层对象与数据库之间的交互,而Spring则是一个全面的后端解决方案,涵盖了依赖注入、AOP(面向切面编程)...
Struts2是基于MVC(Model-View-Controller)设计模式的开源Web应用框架,它解决了传统Struts1的一些问题,提供了更强大的控制结构和更丰富的拦截器。在Struts2.2.3版本中,你可以期待更好的性能和增强的可扩展性,...
SpringSource提供的jar包可能包含了Spring的核心模块,如IoC(Inversion of Control)容器,AOP框架,MVC(Model-View-Controller)支持,以及数据访问层的工具,如JDBC抽象层和ORM集成。 Spring框架的核心概念包括...
Spring是一个全面的后端开发框架,它提供依赖注入(DI)和面向切面编程(AOP),用于简化应用的配置和管理。Spring MVC是Spring框架的一部分,专门处理Web层的请求和响应,负责将用户请求转发到相应的业务逻辑处理。...
2. **Cairngorm**:Cairngorm是Adobe官方推荐的一种轻量级的MVC(Model-View-Controller)设计模式,用于Flex和ActionScript项目。Cairngorm提供了一种结构化的方式来组织代码,使得应用更易于维护和扩展。它包括了...
7:Spring MVC:分离模型、视图、控制器、以便更容易定制 折构函数和虚函数? 答:折构函数式销毁一个类的函数,虚函数是为了C++的动态绑定而设计的。 描述你的编程风格? 答:类名首字母大写,常量一般全部大写,...
- **缓存问题**:Ehcache的配置可能导致缓存失效、更新不及时等问题。 - **资源文件**:国际化消息文件的路径和编码设置不当可能导致国际化功能失效。 在后续的项目中,可以进一步完善这个脚手架,如添加AOP缓存...
1. Spring:Spring是一个全面的后端应用框架,提供了依赖注入(DI)和面向切面编程(AOP)的核心特性。在精简版中,Spring可能通过注解如@Service、@Repository和@Controller来简化bean的定义,避免XML配置。同时,...
Spring MVC(Model-View-Controller)是Spring框架的一部分,专门用于构建Web应用。通过结合CXF和Spring MVC,开发者可以轻松地创建基于HTTP的Web服务,同时利用Spring的强大功能进行依赖注入、AOP(面向切面编程)...
首先,Spring框架是核心,它提供了依赖注入(DI)和面向切面编程(AOP)的功能。DI使得对象之间的依赖关系可以被外部容器管理,而不是硬编码在类内部,这增强了代码的可测试性和可维护性。AOP则允许我们定义横切关注点,...
Struts2、Hibernate和Spring(简称S2SH)是Java Web开发中的一种经典组合,用于构建基于MVC(Model-View-Controller)架构的应用程序。这个整合程序涉及到这三大框架的协同工作,以实现数据持久化、业务逻辑处理以及...