如何使用带注释的控制器实现AOP?
我已经搜索并找到了之前关于这个问题的两篇文章,但似乎无法获得解决方案。
这就是我所拥有的:
调度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
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...");
}
}beforeAnyMethod()适用于不在控制器中的方法;我无法在调用控制器时执行任何东西。我是不是遗漏了什么?
发布于 2012-03-13 22:15:35
为了将方面放在Spring3.1中用@Controller注释的类中的HandlerMethod上,您需要在aspectj-autoproxy元素上拥有proxy-target-class="true"属性。您还需要将CGLIB和ASM库作为WAR/EAR文件中的依赖项。您可以将带注释的方面类指定为bean并使用如上所述的aop:include,也可以省略aop:include并在组件扫描元素中添加类似以下内容的过滤器:
<context:component-scan>
<context:include-filter type="aspectj"
expression="com.your.aspect.class.Here"/>
</context:component-scan>我不知道这是否是Spring3.1的结果,但我知道如果没有这个,你将不能把一个方面放在你的控制器HandlerMethod上。您将得到类似于以下内容的错误:
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]如果方面位于不是控制器的类中的方法上,则不需要执行此操作
发布于 2011-03-15 08:07:17
我将陈述另一种解决方案(对不起,不是直接回答),但您想要做的事情可能最好是通过拦截器和过滤器来完成。
https://stackoverflow.com/questions/5305938
复制相似问题