AOP对SpringMVC的controller失效问题的解决

感谢链接:
http://phoenixfu.iteye.com/blog/2037598

原因:

Spring MVC加载的是WebApplicationContext而不是ApplicationContext,把schema和配置加到spring-mvc.xml中就可以了

beans标签中添加如下信息

xmlns:aop="http://www.springframework.org/schema/aop"

在beans标签的xsi:schemaLocation属性中添加

http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd

完整的beans

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

最后添加:

<aop:aspectj-autoproxy proxy-target-class="true"/>

java类

package com.jsoft.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class ControllerLogAdvice {
    public ControllerLogAdvice(){
        System.out.println();
    }
    @Before("execution(* com.jsoft.ctrls.*.*(..))")
    public void before(JoinPoint jp){
        System.out.println("----------前置通知----------");
        System.out.println(jp.getSignature().getName());
    }

    @After("execution(* com.jsoft.ctrls.*.*(..))")
    public void after(JoinPoint jp){
        System.out.println("----------最终通知----------");
    }
}

原文出处:/show_1EFE7u5ksY0.html