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

博客介绍了Spring AOP的配置与使用。首先要配置所需的jar包,接着配置aop启用注解方式,介绍了<aop:aspectj-autoproxy />的proxy-target-class属性不同值的作用。还给出了切面逻辑代码,指出@Component注解的必要性,并解决了@Pointcut正则表达式写错导致的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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包下所有方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值