spring aop配置遇到的问题及解决方案

1 配置需要的jar包

  <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.13</version>
        </dependency>

2 配置aop启用注解方式

<aop:aspectj-autoproxy />

<aop:aspectj-autoproxy />有一个proxy-target-class属性,默认为false,表示使用jdk动态代理织入增强,

当配为<aop:aspectj-autoproxy  poxy-target-class="true"/>时,表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。

3 切面逻辑代码

@Component
@Aspect
public class AuthorityAspect {

    @Pointcut("execution(* com.alipay.cxbiz.biz.function.*.*(..))")
    public void authorityCheck(){}

    @Before("authorityCheck()")
    public Object before(JoinPoint joinPoint) throws Throwable{
        Object[] args = joinPoint.getArgs();
        for(Object o:args){
            System.out.println(o.toString());
        }
        return null;
    }

}

@Component如果不写该注解是,将不会启动改切面

Caused by: java.lang.IllegalArgumentException: warning no match for this type name: com.alipay.cxbiz.biz.function [Xlint:invalidAbsoluteTypeName]

该错误是因为@Pointcut正则表达式写错,@Pointcut("execution(* com.alipay.cxbiz.biz.function.*(..))")

改为@Pointcut("execution(* com.alipay.cxbiz.biz.function.*.*(..))")

.*.*表示该范围为function包下所有方法

猜你喜欢

转载自blog.csdn.net/u013984781/article/details/95499079