理解Spring AOP
来源:互联网 发布:怎样注册.com域名? 编辑:程序博客网 时间:2024/04/30 19:01
什么是AOP
AOP利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其名为“Aspect”,即方面。所谓“方面”,简单地说,就是将那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可操作性和可维护性。
AOP使用场景
Authentication 权限
Error handling 错误处理
logging记录跟踪
Transactions 事务
AOP相关概念
1.通知(Advice):通知定义了切面是什么以及何时使用。
2.连接点(Joinpoint):
程序能够应用通知的一个“时机”,这些“时机”就是连接点,例如方法被调用时、异常被抛出时等等。
3.切入点(Pointcut)
通知定义了切面要发生的“故事”和时间,那么切入点就定义了“故事”发生的地点,例如某个类或方法的名称
4.切面(Aspect)
通知和切入点共同组成了切面:时间、地点和要发生的“故事”
<aop:config>
<aop:aspect id="TestAspect" ref="aspectBean">
<aop:pointcut id="businessService"
expression="execution(* com.xdy.app.service..*.*(..))" />
<aop:before pointcut-ref="businessService" method="doBefore"/>
<aop:after pointcut-ref="businessService" method="doAfter"/>
<aop:around pointcut-ref="businessService" method="doAround"/>
<aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/>
</aop:aspect>
</aop:config>
<aop:aspect id="TestAspect" ref="aspectBean">
<aop:pointcut id="businessService"
expression="execution(* com.xdy.app.service..*.*(..))" />
<aop:before pointcut-ref="businessService" method="doBefore"/>
<aop:after pointcut-ref="businessService" method="doAfter"/>
<aop:around pointcut-ref="businessService" method="doAround"/>
<aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/>
</aop:aspect>
</aop:config>
AOP切面
public class AspectHandler {
private final static Logger LOG=LoggerFactory.getLogger(AspectHandler.class);
public void doBefore(JoinPoint jp) {
LOG.info("\ncalled for method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long time = System.currentTimeMillis();
Object retVal = pjp.proceed();
time = System.currentTimeMillis() - time;
LOG.info("\nprocess time: " + time + " ms");
return retVal;
}
public void doThrowing(JoinPoint jp, Throwable ex) {
LOG.info("\nI am doThrowing ,throw exception,details:" + ex.getMessage() );
}
public void doAfter(JoinPoint jp) {
LOG.info("\nend method:" + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
}
}
private final static Logger LOG=LoggerFactory.getLogger(AspectHandler.class);
public void doBefore(JoinPoint jp) {
LOG.info("\ncalled for method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long time = System.currentTimeMillis();
Object retVal = pjp.proceed();
time = System.currentTimeMillis() - time;
LOG.info("\nprocess time: " + time + " ms");
return retVal;
}
public void doThrowing(JoinPoint jp, Throwable ex) {
LOG.info("\nI am doThrowing ,throw exception,details:" + ex.getMessage() );
}
public void doAfter(JoinPoint jp) {
LOG.info("\nend method:" + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
}
}
数据响应
2017-02-23 14:17:28,875 INFO [http-bio-80-exec-6] c.x.a.a.h.AspectHandler:doBefore:27 -
called for method: com.xdy.app.service.ApiService.appKey
2017-02-23 14:17:30,880 INFO [http-bio-80-exec-6] c.x.a.a.h.AspectHandler:doAround:34 -
process time: 2005 ms
2017-02-23 14:17:30,880 INFO [http-bio-80-exec-6] c.x.a.a.h.AspectHandler:doAfter:43 -
end method:com.xdy.app.service.ApiService.appKey
called for method: com.xdy.app.service.ApiService.appKey
2017-02-23 14:17:30,880 INFO [http-bio-80-exec-6] c.x.a.a.h.AspectHandler:doAround:34 -
process time: 2005 ms
2017-02-23 14:17:30,880 INFO [http-bio-80-exec-6] c.x.a.a.h.AspectHandler:doAfter:43 -
end method:com.xdy.app.service.ApiService.appKey
1 0
- spring aop的理解
- Spring AOP 理解老
- Spring AOP概念理解
- spring aop 理解
- Spring AOP 理解
- 初步理解spring aop
- Spring中的AOP理解
- Spring AOP的理解
- spring AOP理解
- Spring-理解AOP
- Spring中的AOP理解
- 深入理解Spring AOP
- spring AOP个人理解
- 理解Spring AOP
- spring 的AOP 理解
- spring AOP简单理解
- spring Aop 理解
- Spring AOP的理解
- JMeter-检查点---学习笔记
- Ja进av阶书籍推荐
- amcl说明
- vs2013 创建服务
- struct usb_host_endpoint
- 理解Spring AOP
- IOS图片渲染模式
- 独木舟上的旅行
- B 1003.我要通过
- ImageView 显示图像控件
- Junit4学习笔记
- PAT 1054
- MeasureSpec中三种模式
- 一家反欺诈公司的面试经历——6.JVM底层原理