Overblog Tous les blogs Top blogs Technologie & Science Tous les blogs Technologie & Science
Editer l'article Suivre ce blog Administration + Créer mon blog
MENU

Spring AOP 在工作中的使用,MultiCustomer不同需求管理

15 Juin 2015 Publié dans #spring, #java

使用请注明出处:http://alex2012-c.j.overblog.com/

很多项目使用了Spring,其中AOP(面向切面编程)是很重要的思想。

AOP用处很多,比如事务管理,日志管理等。AOP的使用是为了以更加灵活的方式来解决问题。

工作开发内容是实现多机场管理,不同机场有不同的需求,我们对客户所需要的功能进行了一些parameter的定义,对于哪个机场激活哪些parameter?

此时引进了AOP功能,设计了Feature函数,用Feature来实现对不同机场不同需求的控制。

例如,在系统中定义了function1,function2,function3作为parameter。实际应用中,慕尼黑机场使用到了1和3,尼斯机场使用到了2和3,哥本哈根机场使用了2。

在数据库中,有一个具体的配置表格,对不同机场所需要的功能进行了配置,用true,false表示,方便维护。因为不同机场随时可能对不同功能进行激活或者不激活的选择。

此时,有了Feature,只需要在java代码中对不同的function添加@Feature就可以了。

具体代码例子请耐心。。。

Publicité

1. AOP中几个重要定义

《Spring参考手册》中定义了以下几个AOP的重要概念,结合以上代码分析如下:

  • 切面(Aspect) :官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”,在本例中,“切面”就是类TestAspect所关注的具体行为,例如,AServiceImpl.barA()的调用就是切面TestAspect所关注的行为之一。“切面”在ApplicationContext中<aop:aspect>来配置。
  • 连接点(Joinpoint) :程序执行过程中的某一行为,例如,AServiceImpl.barA()的调用或者BServiceImpl.barB(String _msg, int _type)抛出异常等行为。
  • 通知(Advice) :“切面”对于某个“连接点”所产生的动作,例如,TestAspect中对com.spring.service包下所有类的方法进行日志记录的动作就是一个Advice。其中,一个“切面”可以包含多个“Advice”,例如TestAspect
  • 切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。例如,TestAspect中的所有通知所关注的连接点,都由切入点表达式execution(* com.spring.service.*.*(..))来决定
  • 目标对象(Target Object) :被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,当然在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。
  • AOP代理(AOP Proxy) 在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将 <aop:config> 的 proxy-target-class 属性设为true

2.Spring中AOP的实现

2.1首先要进行配置

在project/src/main/resources/META-INF/spring/context.xml中

Spring AOP 在工作中的使用,MultiCustomer不同需求管理
Publicité
2.2定义Annotation

其中FeatureEnum可以是所有功能的enum,比如function1,function2,function3等。。

DisabledBehaviour 也是一个enum:

* ENUM that holds the list of possible actions in case a feature is disabled
*
* @author caiji
*/
public enum DisabledBehaviour {
SKIP,
FAIL;
}

Spring AOP 在工作中的使用,MultiCustomer不同需求管理
2.3定义切面Bean

/**
* The goal of this aspect is to intercept all methods annotated with @{@link Feature} in order to decide if a given
* Feature is enable/disabled. Based on the annotation values, this aspect may take 3 different actions:
*
* <p>
* Proceed: The aspect will just proceed with the normal flow and the annotated method will be executed.
* <p>
* Skip: The aspect will skip the execution of the annotated method. This is the default behaviour in case the Feature
* is disabled.
* <p>
* Fail: The aspect will throw a @{@link FunctionalException} and the annotated method will not be executed at all.
*/

Spring AOP 在工作中的使用,MultiCustomer不同需求管理
Publicité
2.4完善FeatureService所需函数

此处略去每个函数的实现。

Spring AOP 在工作中的使用,MultiCustomer不同需求管理
2.5调用Annotation

此时就可以写一个函数,并且使用Annotation

Spring AOP 在工作中的使用,MultiCustomer不同需求管理

参考:

http://pandonix.iteye.com/blog/336873/

http://blog.csdn.net/a906998248/article/details/7514969

Partager cet article
Repost0
Pour être informé des derniers articles, inscrivez vous :
Commenter cet article