Java 关于带注释的控制器的 Spring AOP 建议
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3310115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring AOP Advice on Annotated Controllers
提问by jdana
I am trying to use AOP to do some processing after an annotated controller. Everything is running with no errors, but the advice is not being executed.
我正在尝试使用 AOP 在带注释的控制器之后进行一些处理。一切都在运行,没有错误,但没有执行建议。
Here is the controller code:
这是控制器代码:
@Controller
public class HomeController {
@RequestMapping("/home.fo")
public String home(ModelMap model) {
model = new ModelMap();
return "home";
}
}
and the setup in application-config
以及 application-config 中的设置
<aop:aspectj-autoproxy/>
<bean id="testAdvice" class="com.test.TestAdvice">
</bean>
<bean id="testAdvisor"
class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">
<property name="advice" ref="testAdvice" />
<property name="expression" value="execution(* *.home(..))" />
</bean>
and the actual advice
和实际的建议
public class TestAdvice implements AfterReturningAdvice {
protected final Log logger = LogFactory.getLog(getClass());
public void afterReturning(Object returnValue, Method method, Object[] args,
Object target) throws Throwable {
logger.info("Called after returning advice!");
}
}
Is it even possible to have advice on annotated controllers? I am using Spring 2.5.
甚至可以对带注释的控制器提出建议吗?我正在使用 Spring 2.5。
回答by Sasi
For MVC controllers the preferred method of accomplishing what you are trying to do is to use interceptors. See http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-handlermapping-interceptor
对于 MVC 控制器,完成您尝试做的事情的首选方法是使用拦截器。见http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-handlermapping-interceptor
回答by Espen
It's possible to have advice on annotated controllers.
可以对带注释的控制器提出建议。
I assume you want to advice after execution of all methods in classes annotated with @Controller
.
我假设您想在用@Controller
.
Here's an example:
下面是一个例子:
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class ControllerAspect {
@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void controllerBean() {}
@Pointcut("execution(* *(..))")
public void methodPointcut() {}
@AfterReturning("controllerBean() && methodPointcut() ")
public void afterMethodInControllerClass() {
System.out.println("after advice..");
}
}
If you want to use Spring AOP with AspectJ syntax, you also need a configuration file like this:
如果你想使用带有 AspectJ 语法的 Spring AOP,你还需要一个像这样的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="controllerAspect" class="controller.ControllerAspect" />
<aop:aspectj-autoproxy>
<aop:include name="controllerAspect" />
</aop:aspectj-autoproxy>
</beans>
Note: With Spring AOP, the Spring container will only weave Spring beans. If the @Controller
object isn't a Spring bean, you must use AspectJ weaving.
注意:使用 Spring AOP,Spring 容器只会编织 Spring bean。如果@Controller
对象不是 Spring bean,则必须使用 AspectJ 编织。
回答by xli
I had the same problem where advice for Repository was working, but advice for Controller was not. Finally I found a solution. In short, you need to make sure your AOP definition is loaded in Servlet context, not a different context.
我遇到了同样的问题,其中 Repository 的建议有效,但 Controller 的建议无效。最后我找到了解决方案。简而言之,您需要确保在 Servlet 上下文中加载 AOP 定义,而不是在不同的上下文中。
In my case, my Spring AOP definition is defined in tools-config.xml
. After moving it from here
就我而言,我的 Spring AOP 定义是在tools-config.xml
. 从这里移动后
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/tools-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
to here,
到这里,
<servlet>
<servlet-name>petclinic</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/mvc-core-config.xml, classpath:spring/tools-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
the advice for Controller is working.
控制器的建议正在起作用。