aop 的使用

本文介绍了Spring AOP的两种实现方式,包括注解和XML配置。在注解方式中,展示了如何在Spring Boot中启用AOP和定义切面。而在XML配置中,展示了如何配置切面和切入点表达式。注意,当AOP方法内使用Lambda表达式时可能会导致错误。
摘要由CSDN通过智能技术生成

aop有两种方式,一个是注解,另外一个是配置xml

1、注解的方式:

a、和xml一样,首先得有个配置,如果是springboot,写个配置类:

@Configuration
@EnableAspectJAutoProxy
@ComponentScan("xxx.package.xxx")
public class MainConfig {
    //若干代码
}

  切面类

@Aspect
@Component
public class ProcessHibernateSearchResultAspect {
    @SuppressWarnings("unchecked")
    @AfterReturning(returning = "result",pointcut = "execution(* com.xxx.XabcdImpl.get*(..))")
    public void processAfterReturn(Object result) {
       
    }

}

b、如果是spring的话,可以通过xml的配置打开aop的注解:

<aop:aspectj-autoproxy/>

2:使用xml的方式配置aop

public class ProcessHibernateSearchResultAspect {
    @SuppressWarnings("unchecked")
    @AfterReturning(returning = "result",pointcut = "execution(* com.xxx.XabcdImpl.get*(..))")
    public void processAfterReturn(Object result) {
       
    }

}

xml中的配置如下:               

<bean id="processHibernateSearchResultAspect" class="com.xxxx.ProcessHibernateSearchResultAspect"></bean>

            <aop:aspect id="hibernatedaoprocess" ref="processHibernateSearchResultAspect">
                <aop:pointcut expression="execution(* com.xxx.XabcdImpl.get*(..))" id="hibernatePoint"/>
                <aop:after-returning method="processAfterReturn" pointcut-ref="hibernatePoint" returning="result"/>
            </aop:aspect>

3问题:

说明一下使用过程中发现一个问题,如果aop的after等方法中如果有Lambda表达式,那么会报错,猜测动态代理无法再取解析Lambda表达式,使用的时候需要注意了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值