一、问题的现象
1、使用Spring MVC,创建Controller类和@RequestMapping方法
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
ProductServiceImpl productService;
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(name = "pageNum")Integer pageNum, @RequestParam(name = "pageSize")Integer pageSize) throws MyException {
System.out.println("执行findall......");
2、使用spring AOP切面Controller类中的方法
@Component("agentController")
@Aspect
public class AgentController {
private Date visitTime;
private Date endTime;
private int executionTime; //执行时长
private Class clazz; //执行的类
private Method method; //执行的方法
private SysLog sysLog = new SysLog();
@Pointcut("execution(* com.mediacomm.controller.*.*(..))")
public void service(){}
@Autowired
private SysLogService sysLogService;
@Autowired
private HttpServletRequest request;
@Before("service()")
public void beforeAdvice(JoinPoint pro) throws NoSuchMethodException, ParseException {
System.out.println("前置通知......"+sysLog.getId());
3、依靠idea查看前置通知方法切面的目标,是代理成功的
4、但是实际情况执行起来前置通知根本没有切入,控制台没有打印出:前置通知…
二、解决方案
原因:
1、我是基于注解的方式配置的aop,所以在spring的配置文件中需要加入<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
的开启声明
2、但是我在spring的配置文件中又添加了注解扫描忽略
<context:component-scan base-package="com.mediacomm">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
3、因此在spring的容器中并没有Controller类的bean
4、所以导致aop切入点没有找到对应的方法
解决方法:
将<aop:aspectj-autoproxy></aop:aspectj-autoproxy>配置在负责扫描@Controller注解的springmvc的配置文件中,重新运行后控制台输出:
前置通知......null
结论:
需要根据你的切面方法的目标对象的所创建bean实体的配置文件来添加<aop:aspectj-autoproxy></aop:aspectj-autoproxy>