后置通知不报错,前置和环绕都报错我感觉是不是注解扫描的原因云云。
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 使用注解式注入 -->
<context:annotation-config /> <!-- 自动扫描 -->
<context:component-scan base-package="cn.wn"/> <!-- 导入DAO配置 -->
<import resource="spring-dao.xml" /> <!-- 导入数据库配置 -->
<import resource="spring-db.xml" /> <!-- 导入数据库配置 -->
<import resource="spring-tx.xml" /> <!-- 开启aop,对类代理 -->
<aop:config></aop:config> <!-- 开启AOP注解扫描 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
</beans>  spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>    
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"   
    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"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans      
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd      
                        http://www.springframework.org/schema/context      
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd      
                        http://www.springframework.org/schema/mvc      
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">    
                            
    <!--避免IE执行AJAX时,返回JSON出现下载文件 -->    
    <bean id="mappingJacksonHttpMessageConverter"    
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">    
        <property name="supportedMediaTypes">    
            <list>    
                <value>text/html;charset=UTF-8</value>    
            </list>    
        </property>    
    </bean>    
        
     <!-- 添加注解驱动 -->      
    <mvc:annotation-driven />    
    <mvc:default-servlet-handler/>    
        
    <!-- 设置使用注解的类所在的包 -->    
    <context:component-scan base-package="cn.wn" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />

</context:component-scan>  
        
    <!-- 完成请求和注解POJO的映射 -->    
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">    
        <property name="messageConverters">    
            <list>    
                <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->    
            </list>    
        </property>    
    </bean>    
        
    <!-- 定义跳转的文件的前后缀 ,视图模式配置-->    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
        <!-- 这里的配置是自动给后面action的方法return的字符串加上前缀和后缀 -->    
        <property name="prefix" value="/WEB-INF/views/" />    
        <property name="suffix" value=".jsp" />    
    </bean>    
        
    <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器-->    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">      
        <!-- 默认编码 -->    
        <property name="defaultEncoding" value="utf-8" />      
        <!-- 文件大小最大值 -->    
        <property name="maxUploadSize" value="10485760000" />      
        <!-- 内存中的最大值 -->    
        <property name="maxInMemorySize" value="40960" />      
    </bean>     
    
<!-- 开启AOP注解扫描 -->
<aop:aspectj-autoproxy proxy-target-class="true"/> 
</beans>    
package cn.wn.aop;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/**
 * 系统日志
 *
 * @author 
 * @date 2018年3月9日下午10:07:59
 */
@Target({ElementType.PARAMETER, ElementType.METHOD})  
@Retention(RetentionPolicy.RUNTIME)  
@Documented 
public @interface SysLogAop {
String module()  default "";  
    String methods()  default "";
}package cn.wn.advice;import java.lang.reflect.Method;import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import cn.wn.aop.SysLogAop;
import cn.wn.bean.SysLog;
import cn.wn.service.SysLogService;
import cn.wn.util.DateUtils;
import cn.wn.util.IPUtil;@Component("sysLogAdvice")
@Aspect
@Order(1)
public class SysLogAdvice {
@Resource
private SysLogService sysLogService; @Pointcut("@annotation(cn.wn.aop.SysLogAop)")
public void sysLog() {
} /**
 * 
 * 
 * @param jp
 * @param rtv
 * @throws Throwable
 */
@Around("sysLog()")
public Object aroundLog(ProceedingJoinPoint pjp, Object rtv) throws Throwable {
// 操作日志实体
SysLog handleLog = new SysLog();
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
// 用户
handleLog.setUserId("ZhangSan");
// 创建时间
handleLog.setCreateTime(DateUtils.getLongDateStr());
// IP
handleLog.setIp(IPUtil.getRemoteIp(request));
// 方法通知前获取时间,用来计算模块执行时间
long start = DateUtils.nowTimeMillis();
// 拦截的实体类,就是当前正在执行的controller
Object target = pjp.getTarget();
// 拦截的方法名称。当前正在执行的方法
        String methodName = pjp.getSignature().getName();
        // 拦截的方法参数
        Object[] args = pjp.getArgs();
        // 拦截的放参数类型
        Signature sig = pjp.getSignature();
        MethodSignature msig = null;
        if (!(sig instanceof MethodSignature)) {
            throw new IllegalArgumentException("该注解只能用于方法");
        }
        msig = (MethodSignature) sig;
Class[] parameterTypes = msig.getMethod().getParameterTypes();
        Object object = null;
        // 获得被拦截的方法
        Method method = null;
        try {
            method = target.getClass().getMethod(methodName, parameterTypes);
        } catch (NoSuchMethodException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (SecurityException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        if (null != method) {
            // 判断是否包含自定义的注解,说明一下这里的SysLogAop就是我自己自定义的注解
            if (method.isAnnotationPresent(SysLogAop.class)) {
             SysLogAop sysLogAop = method.getAnnotation(SysLogAop.class);
             handleLog.setModule(sysLogAop.module());
             handleLog.setMethod(sysLogAop.methods());
                try {
                    object = pjp.proceed();
                    long end = DateUtils.nowTimeMillis();
                    //将计算好的时间保存在实体中
                    handleLog.setResponseDuration(""+(end-start));
                    handleLog.setCommite("执行成功!");
                    //保存进数据库
                    sysLogService.saveObject(handleLog);
                } catch (Throwable e) {
                 long end = DateUtils.nowTimeMillis();
                    //将计算好的时间保存在实体中
                    handleLog.setResponseDuration(""+(end-start));
                    handleLog.setCommite("执行失败!");
                    //保存进数据库
                    sysLogService.saveObject(handleLog);
                }
            } else {//没有包含注解
                object = pjp.proceed();
            }
        } else { //不需要拦截直接执行
            object = pjp.proceed();
        }
        return object;
}
}
package cn.wn.controller;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;import cn.wn.aop.SysLogAop;/**
 * 用户控制层
 * 
 * @author 
 *
 */
@Controller
@RequestMapping(value = "/user")
public class UserController { @RequestMapping("/sysLog")
@SysLogAop(module="系统日志",methods="查询用户")
public void sysLog(Model model, HttpServletRequest request) {

}
}

解决方案 »

  1.   


    把XML  Schema调整一下,可以尝试如下:
    <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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context-4.0.xsd
                http://www.springframework.org/schema/aop 
                http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                http://www.springframework.org/schema/tx 
                http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                http://www.springframework.org/schema/mvc 
                http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context-4.0.xsd">仅供参考