AOP与OOP

AOP与OOP,面向切面编程和面向对象编程。OOP面向的是对象,针对实体,像类,对象及其属性和行为进行抽象封装,AOP面向功能,是针对切面进行功能提取,处理过程的某个步骤或阶段,像权限检查,日志操作,日志记录,性能统计,安全控制,事务处理,异常处理等,解决特定领域问题的代码从业务逻辑中独立出来。两者互补。定义切面,切面里面包含切入点和通知,切入点内是一组连接点,切入点内定义前置通知,后置通知,环绕通知,最后将切面织入代理的目标对象。AOP实现原理是java动态代理,但是jdk的动态代理必须实现接口,所以spring的aop是用cglib这个库实现的,cglis使用里asm这个直接操纵字节码的框架,所以可以做到不使用接口的情况下实现动态代理。OOP具有封装,继承,多态等东西来定义从上到下这种层次关系,但要想实现从左到右的关系的话就开始有点水土不服了,AOP避免了那些与我们要实现的核心功能不大有关系的东西散布在我们代码的周边,显示十分不好看。AOP就是将这些与主体业务无关,但又有为业务提供服务的逻辑代码封装起来,横向切入。AOP允许用正则表达式来定义切点

aop正则表达式匹配切入点execution:
任意公共方法的执行: 
execution(public * *(..)) 
任何一个以“set”开始的方法的执行: 
execution(* set*(..)) 
AccountService 接口的任意方法的执行: 
execution(* com.xyz.service.AccountService.*(..)) 
定义在service包里的任意方法的执行: 
execution(* com.xyz.service.*.*(..)) 
定义在service包或者子包里的任意方法的执行: 
execution(* com.xyz.service..*.*(..)) 
在service包里的任意连接点(在Spring AOP中只是方法执行) : 
within(com.xyz.service.*) 
在service包或者子包里的任意连接点(在Spring AOP中只是方法执行) : 
within(com.xyz.service..*) 
实现了 AccountService 接口的代理对象的任意连接点(在Spring AOP中只是方法执行) : 
this(com.xyz.service.AccountService) 
'this'在binding form中用的更多:- 请常见以下讨论通知的章节中关于如何使得代理对象可以在通知体内访问到的部分。 
实现了 AccountService 接口的目标对象的任意连接点(在Spring AOP中只是方法执行) : 
target(com.xyz.service.AccountService) 
'target'在binding form中用的更多:- 请常见以下讨论通知的章节中关于如何使得目标对象可以在通知体内访问到的部分。 
任何一个只接受一个参数,且在运行时传入的参数实现了 Serializable 接口的连接点 (在Spring AOP中只是方法执行) 
args(java.io.Serializable) 
'args'在binding form中用的更多:- 请常见以下讨论通知的章节中关于如何使得方法参数可以在通知体内访问到的部分。 请注意在例子中给出的切入点不同于 execution(* *(java.io.Serializable)): args只有在动态运行时候传入参数是可序列化的(Serializable)才匹配,而execution 在传入参数的签名声明的类型实现了 Serializable 接口时候匹配。 
有一个 @Transactional 注解的目标对象中的任意连接点(在Spring AOP中只是方法执行) 
@target(org.springframework.transaction.annotation.Transactional) 
'@target' 也可以在binding form中使用:请常见以下讨论通知的章节中关于如何使得annotation对象可以在通知体内访问到的部分。 
任何一个目标对象声明的类型有一个 @Transactional 注解的连接点(在Spring AOP中只是方法执行) 
@within(org.springframework.transaction.annotation.Transactional) 
'@within'也可以在binding form中使用:- 请常见以下讨论通知的章节中关于如何使得annotation对象可以在通知体内访问到的部分。 
任何一个执行的方法有一个 @Transactional annotation的连接点(在Spring AOP中只是方法执行) 
@annotation(org.springframework.transaction.annotation.Transactional) 
'@annotation' 也可以在binding form中使用:- 请常见以下讨论通知的章节中关于如何使得annotation对象可以在通知体内访问到的部分。 
任何一个接受一个参数,并且传入的参数在运行时的类型实现了 @Classified annotation的连接点(在Spring AOP中只是方法执行) 
@args(com.xyz.security.Classified)

@Around("execution(* com.newsoft.mobile.controller.GreenVehicleController.uploadGreenVehicleData(..))||"
+ "execution(* com.newsoft.mobile.controller.GreenVehicleController.isGreenVehicleDataUpload(..))")
(* com.evan.crm.service.*.*(..))中几个通配符的含义: 
|第一个 * —— 通配 随便率性返回值类型| 
|第二个 * —— 通配包com.evan.crm.service下的随便率性class| 
|第三个 * —— 通配包com.evan.crm.service下的随便率性class的随便率性办法| 
|第四个 .. —— 通配 办法可以有0个或多个参数| 


<!-- 配置那些类的方法进行事务管理 --> 
<aop:config> 
<aop:pointcut id="allServiceMethod" expression="execution (* com.cms.sys.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceMethod" /> 
</aop:config> 


还有一个 
execution (* com.cms.art.service.*.*(..))" 
要怎么写?
可以这样写:将execution分开写。 
<aop:config> 
<aop:pointcut id="allServiceMethod" expression="(execution (* com.cms.sys.service.*.*(..)))or (execution (* com.cms.art.service.*.*(..)))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceMethod" /> 
</aop:config> 
例如:
<!-- Hibernate 事务控制配置 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="nestedTransactionAllowed" value="true" />
</bean>
<!-- 使用Hibernate的事务控制器管理事务 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" rollback-for="Exception" propagation="REQUIRED" />
<tx:method name="update*" rollback-for="Exception" propagation="REQUIRED" />
<tx:method name="del*" rollback-for="Exception" propagation="REQUIRED" />
<tx:method name="remove*" rollback-for="Exception" propagation="REQUIRED" />
<tx:method name="add*" rollback-for="Exception" propagation="REQUIRED" />
<tx:method name="insert*" rollback-for="Exception" propagation="REQUIRED" />
<tx:method name="auth*" rollback-for="Exception" propagation="REQUIRED" />
<tx:method name="*" rollback-for="Exception" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 使用对实体类的事务控制 -->
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.rf..*.*Service.*(..))" advice-ref="txAdvice" />
<aop:advisor pointcut="execution(* com..*.*Service.*(..))" advice-ref="txAdvice" />
</aop:config>


 

posted @ 2016-08-31 16:34  海的心  阅读(298)  评论(0编辑  收藏  举报