SpirngMVC AOP 用注解方式配置切面及IllegalArgumentException: error at ::0 formal unbound in pointcut 异常分析

MEAVN代码

<!-- springAOP核心包 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.0.2.RELEASE</version>
</dependency>
 <!--使用AspectJ方式注解需要相应的包--> 
<dependency>  
    <groupId>org.aspectj</groupId>  
    <artifactId>aspectjrt</artifactId>  
    <version>1.8.9</version>  
</dependency>  
<dependency>  
    <groupId>org.aspectj</groupId>  
    <artifactId>aspectjweaver</artifactId>  
    <version>1.8.9</version>  
</dependency> 

注意:此处版本号一定要与JDK兼容 否则会报error at ::0 can't find referenced pointcut pointCutName 错误

spring-mvc.xml 配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/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.xsd
">

<!-- 启用AsjectJ支持 -->
<aop:aspectj-autoproxy proxy-target-class="true" />

注意:下划线是必须配置的项

注解类配置

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 采用注解形式进行AOP定义*/
@Aspect    //该标签把LoggerAspect类声明为一个切面
@Component //该标签把LoggerAspect类放到IOC容器中
public class LogAopAction {

    /**
     * execution:用来匹配执行方法的连接点
     * A:@Pointcut("execution(* com.aijava.springcode.service..*.*(..))")
     * 第一个*表示匹配任意的方法返回值,..(两个点)表示零个或多个,上面的第一个..表示service包及其子包,第二个*表示所有类,第三个*表示所有方法,第二个..表示方法的任意参数个数
     * B:@Pointcut("within(com.aijava.springcode.service.*)")
     * within限定匹配方法的连接点,上面的就是表示匹配service包下的任意连接点
     * C:@Pointcut("this(com.aijava.springcode.service.UserService)")
     * this用来限定AOP代理必须是指定类型的实例,如上,指定了一个特定的实例,就是UserService
     * D:@Pointcut("bean(userService)")
     * bean也是非常常用的,bean可以指定IOC容器中的bean的名称
     * @within(org.springframework.stereotype.Service),拦截带有 @Service 注解的类的所有方法
   * @annotation(org.springframework.web.bind.annotation.RequestMapping),拦截带有@RquestMapping的注解方法
     */
    @Pointcut("execution(* com.cn.xxx..*.*(..))")//把指定包路径下所有方法都进行匹配
    private void pointCut() {
    }

    /**
     * 方法开始执行
     */
    @Before("pointCut()")
    public void doBefore(JoinPoint point) {

        Class clazz = point.getTarget().getClass();
        String targetName = clazz.getSimpleName();
        String methodName = point.getSignature().getName();
        
        Logger logger = Logger.getLogger(clazz);
        Object[] args = point.getArgs();
        StringBuilder formatLog = new StringBuilder(
                "进入方法 " + clazz.getPackage().getName() + targetName + "." + methodName + ",参数是:");
        for (Object ignored : args) {
            formatLog.append("{},");
        }
        logger.info(formatLog.toString()+args);
    }/**
     * 方法结束执行后的操作
     */
    @AfterReturning(pointcut = "pointCut()", returning = "returnVal")
    public void doAfter(JoinPoint point, Object returnVal) {
        Class clazz = point.getTarget().getClass();
        String targetName = clazz.getSimpleName();
        String methodName = point.getSignature().getName();
        String logString = "退出方法: " + targetName + "." + methodName + ",返回结果是: " + returnVal;
        Logger logger = Logger.getLogger(clazz);
        logger.info(logString);

    }

    /**
     * 方法有异常时的操作
     */
    @AfterThrowing(pointcut = "pointCut()", throwing = "error")
    public void doAfterThrow(JoinPoint point, Throwable error) {

        Class clazz = point.getTarget().getClass();
        String targetName = clazz.getSimpleName();
        String methodName = point.getSignature().getName();
        Logger logger = Logger.getLogger(clazz);
        String logString = " 异常发生在方法: " + targetName + "." + methodName + ",异常为 : ";
        logger.error(logString, error);
    }

    /**
     * AOP环绕方法执行
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("pointCut()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        try 
        {
            proceedingJoinPoint.proceed();
        }catch (Exception e) {
            e.toString();
        }
        

    }

}

注意:红色部分的一定要对应如果不对应就会报 IllegalArgumentException: error at ::0 formal unbound in pointcut 错误 ,这个错误我排查了好久发现,项目一直启动错误,切记参数一定要对应.

 

posted @ 2017-12-29 10:14  疯癫大圣  阅读(2426)  评论(0编辑  收藏  举报