我想在Spring(3.2.3)@Controller中的每个方法之前运行一些代码.我有以下定义,但它不会运行.我怀疑切入点表达式是不正确的.
调度员servlet.xml中
<aop:aspectj-autoproxy/>
<bean class="com.example.web.controllers.ThingAspect"/>
Run Code Online (Sandbox Code Playgroud)
cewcThingAspect
@Pointcut("execution(com.example.web.controllers.ThingController.*(..))")
public void thing() {
}
@Before("thing()")
public void doStuffBeforeThing(JoinPoint joinPoint) {
// do stuff here
}
Run Code Online (Sandbox Code Playgroud) 如何使用带注释的控制器实现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>
Run Code Online (Sandbox Code Playgroud)
控制器:
@Controller
public class FooController {
@RequestMapping(value="/index.htm", method=RequestMethod.GET)
public String showIndex(Model model){
return "index";
}
}
Run Code Online (Sandbox Code Playgroud)
方面:
@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() && …Run Code Online (Sandbox Code Playgroud)