注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

世界的瞭望哨

认识自己 认识世界

 
 
 

日志

 
 

简介Spring AOP(转载 AOP with Spring Framework)  

2014-05-25 23:07:28|  分类: Web开发 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

原文传送门:

http://www.tutorialspoint.com/spring/aop_with_spring.htm

一句话注解:

Pointcut就是一把刀,一刀下去,可以切中一组Target Objects,Target Objects被切中的地方就是Join point,Advice就是在Join point处被执行


One of the key components of Spring Framework is the Aspect oriented programming (AOP)framework. Aspect Oriented Programming entails breaking down program logic into distinct parts called so-called concerns. The functions that span multiple points of an application are called cross-cutting concerns and these cross-cutting concerns are conceptually separate from the application's business logic. There are various common good examples of aspects like logging, auditing, declarative transactions, security, and caching etc.

The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Dependency Injection helps you decouple your application objects from each other and AOP helps you decouple cross-cutting concerns from the objects that they affect. AOP is like triggers in programming languages such as Perl, .NET, Java and others.

Spring AOP module provides interceptors to intercept an application, for example, when a method is executed, you can add extra functionality before or after the method execution.

AOP Terminologies:

Before we start working with AOP, let us become familiar with the AOP concepts and terminology. These terms are not specific to Spring, rather they are related to AOP.

TermsDescription
AspectA module which has a set of APIs providing cross-cutting requirements. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement.
Join pointThis represents a point in your application where you can plug-in AOP aspect. You can also say, it is the actual place in the application where an action will be taken using Spring AOP framework.
AdviceThis is the actual action to be taken either before or after the method execution. This is actual piece of code that is invoked during program execution by Spring AOP framework.
PointcutThis is a set of one or more joinpoints where an advice should be executed. You can specify pointcuts using expressions or patterns as we will see in our AOP examples.
IntroductionAn introduction allows you to add new methods or attributes to existing classes.
Target objectThe object being advised by one or more aspects, this object will always be a proxied object. Also referred to as the advised object.
WeavingWeaving is the process of linking aspects with other application types or objects to create an advised object. This can be done at compile time, load time, or at runtime.

Types of Advice

Spring aspects can work with five kinds of advice mentioned below:

AdviceDescription
beforeRun advice before the a method execution.
afterRun advice after the a method execution regardless of its outcome.
after-returningRun advice after the a method execution only if method completes successfully.
after-throwingRun advice after the a method execution only if method exits by throwing an exception.
aroundRun advice before and after the advised method is invoked.

Custom Aspects Implementation

Spring supports the @AspectJ annotation style approach and the schema-based approach to implement custom aspects. These two approaches have been explained in detail in the following two sub chapters

ApproachDescription
XML Schema basedAspects are implemented using regular classes along with XML based configuration.
@AspectJ based@AspectJ refers to a style of declaring aspects as regular Java classes annotated with Java 5 annotations.

  评论这张
 
阅读(460)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018