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

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念