您的位置:首页 > 编程语言 > ASP

spring-aop---AspectJ注解式切面编程。

2017-10-01 15:08 387 查看
SpringAOP中是直接用AspectJ的的注解来使用,这些注解是AspectJ5之后引入的特性。如不做其他设置,注解方式依旧是用的JdkDynamicAopProxy类来生成实现类,面向接口生成。因为注解式属于AspectJ体系的,因此我们需要在配置文件中手动注册一个
AnnotationAwareAspectJAutoProxyCreator
bean来将带有@Aspect注解的bean在编译器动态生成代理类,将切面织入到代理类中。
publicinterfaceUser{publicvoidsay();}
@ComponentpublicclassUserImplimplementsUser{publicvoidsay(){System.out.println("helloaop!");}}
@Aspect//注解属于AspectJ@Component//在spring中注册为bean,纳入管理publicclassAnnoAspect{@Pointcut("execution(*demo.entity.User.*(..))")publicvoidpointcut(){};@Before("pointcut()")publicvoidbefore(){System.out.println("beforerun...");}@After("pointcut()")publicvoidafter(){System.out.println("afterrun...");}@Around("pointcut()")publicvoidaround(ProceedingJoinPointproceedingJoinPoint)throwsThrowable{proceedingJoinPoint.proceed();System.out.println("aroundrun...");}@AfterThrowing("pointcut()")publicvoidthrowEx(){System.out.println("throwex...");}}
ApplicationContextapplicationContext=newClassPathXmlApplicationContext("/spring.xml");//getBean这次直接返回动态代理类Useruser=(User)applicationContext.getBean("userImpl");user.say();
输出
beforerun...helloaop!aroundrun...afterrun...
注意我们依旧是用的接口来接受代理类。
然后看配置文件中这一个类名是太长了。。因此spring又对此进行了优化,只需要开启某个配置即可替换这个注册这个bean。
<aop:aspectj-autoproxy>表示开启AspectJ的面向接口的动态代理生成,而不再使用spring的动态代理方式,区别就是一个是编译时生成代理类,一个是运行时生成代理类。
输出
beforerun...helloaop!aroundrun...afterrun...
现在依旧是面向接口的,我们要使用CGLib子类代理呢。像昨天那样一样作就行了。
成功输出
beforerun...helloaop!aroundrun...afterrun...
然后仔细看我们的配置文件,以下配置和我们昨天的配置是一样的,因此我们就可以得出如下结论。
<aop:aspectj-autoproxyproxy-target-class="true"/>
1.spring声明式和变成式在运行时,动态生成一个面向接口的代理子类。
2.spring的注解式是使用了AspectJ的注解,必须使用<aop:aspectj-autoproxy/>标签开启对AspectJ的支持。
在编译时,spring动态生成一个面向接口的代理子类。
3.不管是spring还是AspectJ如果要使用CGLib代理开启<aop:aspectj-autoproxyproxy-target-class="true"/>标签。
在编译时,spring动态生成一个实现类的代理子类。

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 面向切面 spring