Spring    AOP1

来源:互联网 发布:win10 企业版激活软件 编辑:程序博客网 时间:2024/05/28 03:03

主题三  AOP
.AOP:Aspect OrientedProgramming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同意维护的一种技术
.主要功能是:日志记录,性能统计,安全控制,事务处理,异常处理等等

Spring <wbr> <wbr> <wbr> <wbr>AOP1



AOP实现方式
.预编译
 - AspectJ
.运行期动态代理(JDK动态代理,CGLib动态代理)
 - SpringAOP,JbossAOP


AOP几个相关概念:
Spring <wbr> <wbr> <wbr> <wbr>AOP1



Advice(通知)的类型:在切面的某个特定点上执行的动作
Spring <wbr> <wbr> <wbr> <wbr>AOP1


Spring框架中AOP的用途
.提供了声明式的企业服务,特别是EJB的替代服务声明
.允许用户定制自己的方面,以完成OOP(面向对象)与AOP(横切)的互补使用


Spring的AOP实现
.纯java实现,无需特殊的编译过程,不需要控制类加载器层次
.目前只支持方法执行连接点(通知Spring Bean的方法执行)
.不是为了提供完整的AOP实现;而是侧重于提供一种SOP实现和SpringIoC容器之间的整合,用于帮助解决企业应用中的常见问题
.Spring AOP不会与AspectJ竞争,从而提供综合全面的AOP解决方案


有接口和无接口的Spring AOP实现区别
.Spring AOP默认使用标准的JavaSE动态代理作为AOP代理,这使得任何接口(或接口集)都可以被代理
.SpringAOP中也可以使用CGLIB代理(如果一个业务对象并没有实现一个接口)


基于Schema-based的AOP实现
Spring所有切面和通知器都必须放在一个《aop:config》内(可以配置包含多个《aop:config》元素),每一个《aop:config》可以包含pointcut,advisor和aspect元素(必须按照这个顺序进行相应的配置)
《aop:config》风格的配置大量使用了Spring的自动代理机制




aspect
.《aop:config》
.《aop:aspect》
《aop:config》
《aop:aspect id="myAspect" ref="aBean"》
...
《/aop:aspect》
《/aop:config》
《bean id="aBean" calss=""》
    ...
《/bean》



pointcut
Spring <wbr> <wbr> <wbr> <wbr>AOP1
Spring <wbr> <wbr> <wbr> <wbr>AOP1
Advice应用:

.befor advice:
例子:
spring-aop-schema-advice.xml:
Spring <wbr> <wbr> <wbr> <wbr>AOP1

public class MoocAspect {
public void before() {
System.out.println("MoocAspect before.");
}
}

public class AspectBiz {
public void biz() {
System.out.println("AspectBiz biz.");
}
}

@RunWith(BlockJUnit4ClassRunner.class)
public class TestAOPSchemaAdvice extends UnitTestBase {
public TestAOPSchemaAdvice() {
super("classpath:spring-aop-schema-advice.xml");
}
@Test
public void testBiz() {
AspectBiz biz = super.getBean("aspectBiz");
biz.biz();
}

结果:
MoocAspect before.
AspectBiz biz.
Spring <wbr> <wbr> <wbr> <wbr>AOP1






.After returning advice

《aop:after-returningmethod="afterReturning"pointcut-ref="moocPiontcut"/》 
在MoocAspect添加方法:
public void afterReturning(){
System.out.println("MoocAspectafterReturning.");
}


执行测试类:
MoocAspect before.
AspectBiz biz.
MoocAspect afterReturning.


.After throwing advice
可使用throwing属性来指定可被传递的异常的参数名称


《aop:after-throwingmethod="afterThrowing"pointcut-ref="moocPiontcut"/》 
public void afterThrowing() {
System.out.println("MoocAspectafterThrowing.");
}

public class AspectBiz {
public void biz() {
System.out.println("AspectBizbiz.");
throw new RuntimeException();  //添加抛出异常的语句
}
}

结果:
MoocAspect before.
AspectBiz biz.
MoocAspect afterThrowing.
我们发现afterreturning不见啦,因为执行过程抛出了异常,是没有办法正常返回的,这里将throw newRuntimeException();注释掉,结果:
MoocAspect before.
AspectBiz biz.
MoocAspect afterReturning.



.After(finally) advice

《aop:after method="after"pointcut-ref="moocPiontcut"/》 
public void after() {
System.out.println("MoocAspectafter.");
}

结果:
MoocAspect before.
AspectBiz biz.
MoocAspect afterReturning.
MoocAspect after.方法结束前的最后一句代码

如果放开throw newRuntimeException();的注释:
MoocAspect before.
AspectBiz biz.
MoocAspect afterThrowing.
MoocAspect after.
即无论方法是否正常结束,after都会被执行。




.Around advice
通知方法的第一个参数必须是ProceedingJoinPoint类型

《aop:around method="around"pointcut-ref="moocPiontcut"/》 

在类MoocAspect中添加方法:
public Objectaround(ProceedingJoinPoint pjp) {
Object obj = null;
try {
System.out.println("MoocAspectaround 1.");
obj = pjp.proceed();
System.out.println("MoocAspectaround 2.");
} catch (Throwable e) {
e.printStackTrace();
}
return obj;
}


测试结果:
MoocAspect before.
MoocAspect around 1.
AspectBiz biz.
MoocAspect around 2.
MoocAspect after.
MoocAspect afterReturning.



.Advice parameters

《aop:around method="around"pointcut-ref="moocPiontcut"/》 

《aop:around method="aroundInit"pointcut="execution(*com.imooc.aop.schema.advice.biz.AspectBiz.init(String,int)) 
and args(bizName,times)"/》 

类AspectBiz:
public void init(String bizName,int times) {
System.out.println("AspectBiz init: " + bizName + "   " + times);
}


类MoocAspect:
public ObjectaroundInit(ProceedingJoinPoint pjp, String bizName, int times){
System.out.println(bizName + "  " + times);
Object obj = null;
try {
System.out.println("MoocAspectaroundInit 1.");
obj = pjp.proceed();
System.out.println("MoocAspectaroundInit 2.");
} catch (Throwable e) {
e.printStackTrace();
}
return obj;
}


测试类:
@Test
public void testInit() {
AspectBiz biz =super.getBean("aspectBiz");
biz.init("moocService", 3);
}


结果:
MoocAspect before.
MoocAspect around 1.
moocService  3
MoocAspect aroundInit 1.
AspectBiz init : moocService  3
MoocAspect aroundInit 2.
MoocAspect around 2.
MoocAspect after.
MoocAspect afterReturning.


如果将配置文件中其他配置注释掉,只留下《aop:aroundmethod="aroundInit" pointcut="execution(*com.imooc.aop.schema.advice.biz.AspectBiz.init(String,int)) andargs(bizName, times)"/》 
结果为:
moocService  3
MoocAspect aroundInit 1.
AspectBiz init : moocService  3
MoocAspect aroundInit 2.



0 0
原创粉丝点击