在Spring 3中为控制器实现AOP [英] implement AOP for Controllers in Spring 3
问题描述
如何使用注释的控制器实现AOP?
我搜索并发现了两个有关该问题的帖子,但似乎无法获得解决方案工作。
这里是我有:
Dispatch Servlet:
< beans xmlns =http: //www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xmlns:p =http:// www.springframework.org/schema/p
xmlns:context =http://www.springframework.org/schema/context
xmlns:mvc =http://www.springframework。 org / schema / mvc
xmlns:aop =http://www.springframework.org/schema/aop
xsi:schemaLocation =
http://www.springframework。 org / schema / beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework .org / schema / context / spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0 .xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd\">
< context:annotation-config />
< context:component-scan base-package =com.foo.controller/>
< bean id =fooAspectclass =com.foo.aop.FooAspect/>
< aop:aspectj-autoproxy>
< aop:include name =fooAspect/>
< / aop:aspectj-autoproxy>
< / beans>
控制器:
code> @Controller
public class FooController {
@RequestMapping(value =/ index.htm,method = RequestMethod.GET)
public String showIndex模型){
returnindex;
}
}
Aspect:
@Aspect
public class FooAspect {
@Pointcut(@ target(org.springframework.stereotype.Controller))
public void controllerPointcutter(){}
@Pointcut(execution(* *(..)))
public void methodPointcutter(){}
@Before(controllerPointcutter())
public void beforeMethodInController(JoinPoint jp){
System.out.println(## controller before controller call ...);
}
@AfterReturning(controllerPointcutter()&& methodPointcutter())
public void afterMethodIncontroller(JoinPoin jp){
System.out.println (### after returning ...);
}
@Before(methodPointcutter())
public void beforeAnyMethod(JoinPoint jp){
System.out.println(### before any呼叫...);
}
}
beforeAnyMethod ;我不能得到任何东西执行对控制器的调用。
为了把一个方面放在用注释的类中的HandlerMethod上, @Controller
在Spring 3.1中,您需要在方面具有
元素。您还需要将CGLIB和ASM库作为WAR / EAR文件中的依赖关系。您可以将注释的方面类指定为bean,并使用上面所述的 proxy-target-class =true
autoproxy aop:include
,也可以保留 aop:include
out并在组件扫描元素中添加与此类似的过滤器:
< context:component-scan>
< context:include-filter type =aspectj
expression =com.your.aspect.class.Here/>
< / context:component-scan>
我不知道这是否是一个要求作为Spring 3.1的结果,但我知道,这个,你不能把一个方面放在你的控制器HandlerMethod上。您将收到类似于以下错误的错误:
原因:java.lang.IllegalArgumentException:object不是声明类
HandlerMethod详细信息:
Controller [$ Proxy82]
方法[public void com.test.TestController.testMethod(java.security.Principal,javax.servlet.http.HttpServletResponse)throws javax.servlet。 ServletException]
已解析的参数:
[0] [null]
[1] [type = com.ibm.ws.webcontainer.srt.SRTServletResponse] [value = com.ibm.ws。 webcontainer.srt.SRTServletResponse@dcd0dcd]
如果您的方面是在不是控制器的类
How do I implement AOP with an annotated Controller?
I've search and found two previous posts regarding the problem, but can't seem to get the solutions to work.
Here's what I have:
Dispatch Servlet:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.foo.controller"/>
<bean id="fooAspect" class="com.foo.aop.FooAspect" />
<aop:aspectj-autoproxy>
<aop:include name="fooAspect" />
</aop:aspectj-autoproxy>
</beans>
Controller:
@Controller
public class FooController {
@RequestMapping(value="/index.htm", method=RequestMethod.GET)
public String showIndex(Model model){
return "index";
}
}
Aspect:
@Aspect
public class FooAspect {
@Pointcut("@target(org.springframework.stereotype.Controller)")
public void controllerPointcutter() {}
@Pointcut("execution(* *(..))")
public void methodPointcutter() {}
@Before("controllerPointcutter()")
public void beforeMethodInController(JoinPoint jp){
System.out.println("### before controller call...");
}
@AfterReturning("controllerPointcutter() && methodPointcutter() ")
public void afterMethodInController(JoinPoin jp) {
System.out.println("### after returning...");
}
@Before("methodPointcutter()")
public void beforeAnyMethod(JoinPoint jp){
System.out.println("### before any call...");
}
}
The beforeAnyMethod() works for methods NOT in a controller; I cannot get anything to execute on calls to controllers. Am I missing something?
In order to put an aspect on a HandlerMethod in a class annotated with @Controller
in Spring 3.1 you need to have the proxy-target-class="true"
attribute on the aspectj-autoproxy
element. You also need to have the CGLIB and the ASM libraries as a dependency in your WAR/EAR file. You can either specify your annotated aspect class as a bean and use the aop:include
as stated above or you can leave the aop:include
out and add a filter similar to this in your component scan element:
<context:component-scan>
<context:include-filter type="aspectj"
expression="com.your.aspect.class.Here"/>
</context:component-scan>
I do not know if this is a requirement as a result of Spring 3.1 only but I know that without this, you will not be able to put an aspect on your controller HandlerMethod. You would get an error similar to:
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
HandlerMethod details:
Controller [$Proxy82]
Method [public void com.test.TestController.testMethod(java.security.Principal,javax.servlet.http.HttpServletResponse) throws javax.servlet.ServletException]
Resolved arguments:
[0] [null]
[1] [type=com.ibm.ws.webcontainer.srt.SRTServletResponse] [value=com.ibm.ws.webcontainer.srt.SRTServletResponse@dcd0dcd]
This is not required if your aspect is on a method in a class that is not a controller
这篇关于在Spring 3中为控制器实现AOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!