微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

aop 的使用

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表达式,使用的时候需要注意了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。