AOP的一些东西(事务的传播特性,前置,后置,环绕,异常 等各种通知)

1、事务的配置

 

基本的六种事务传播:

1. PROPAGATION_REQUIRED: 如果存在一个事务,则支持当前事务。如果没有事务则开启(常用的配置)
2. PROPAGATION_SUPPORTS: 如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行
3. PROPAGATION_MANDATORY: 如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。
4. PROPAGATION_REQUIRES_NEW: 总是开启一个新的事务。如果一个事务已经存在,则将这个存在的事务挂起。
5. PROPAGATION_NOT_SUPPORTED: 总是非事务地执行,并挂起任何存在的事务。
6. PROPAGATION_NEVER: 总是非事务地执行,如果存在一个活动事务,则抛出异常

Spring配置声明式事务:

* 配置SessionFactory
* 配置事务管理器
* 事务的传播特性
* 那些类那些方法使用事务

2、编写业务逻辑方法
* 继承HibernateDaoSupport类,使用HibernateTemplate来持久化,HibernateTemplate是
   Hibernate Session的轻量级封装
* 默认情况下运行期异常才会回滚(包括继承了RuntimeException子类),普通异常是不会滚的
* 编写业务逻辑方法时,最好将异常一直向上抛出,在表示层(struts)处理
* 关于事务边界的设置,通常设置到业务层,不要添加到Dao上

<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
     <value>classpath:hibernate.cfg.xml</value>
    </property>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
     <tx:method name="add*" propagation="REQUIRED"/>
     <tx:method name="del*" propagation="REQUIRED"/>
     <tx:method name="modify*" propagation="REQUIRED"/>
     <tx:method name="*" propagation="REQUIRED" read-only="true"/>
    </tx:attributes>
</tx:advice>

<!-- 哪些类哪些方法使用事务 -->
<aop:config>
    <aop:pointcut expression="execution(* com.biped.service.*.*(..))" id="transactionPC"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPC"/>
</aop:config>

<!-- 普通IOC注入 -->
<bean id="userManager" class="com.biped.service.UserManagerImpl">
    <property name="logManager" ref="logManager"/>
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="logManager" class="com.biped.service.LogManagerImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>


2、声明一个切面

              在基于AOP命名空间的Spring AOP中,要声明一个切面,需要使用<aop:config/>的子标签<aop:aspect>。<aop:aspect>标签有一个ref属性必须被赋值,它用于指定和该切面关联的受管Bean(backing bean,以后我们都将使用Backing Bean来称呼这样的Bean)。正如下例所示,该Bean对应的java类是一个普通的java类,在该类中定义了切面的通知方法。此外,<aop:aspect>标签还有两个可选的order属性和id属性,order属性用于指定该切面的加载顺序,id属性用于标识该切面。范例如下:简单的说就是来声明处理日志,事务等方法的来源

Xml代码
<bean id="MyAspect" class="aopTest.TestAround" /> //一个环绕通知的实现类的定义 <!-- 配置一个环绕通知 --> <aop:aspect ref="MyAspect" order="0" id="Test"> <aop:pointcut id="testPointcut" expression="execution(* action.*.*(..))" /> <aop:around pointcut-ref="testPointcut" method="around"/> </aop:aspect>

 

3、声明一个切入点
      要声明一个切入点,可以使用<aop:aspect>的子标签<aop:pointcut>,在Spring2.5中它有两个属性id和expression,分别用于标示该切入点和设定该切入点表达式。例如:

 

Xml代码

<?xml version="1.0" encoding="UTF-8"?> <beans ……> <bean id="MyAspect" class="aop.test.MyAspect"/> <aop:config proxy-target-class="true"> <aop:aspect ref="MyAspect" order="1" id=”TestAspectName”> <aop:pointcut id="test" expression="execution(* aop.test.TestBean.*(..))"/> <aop:before pointcut="aop.test.MyAspect.Pointcut1()" method="beforeAdvice" /> </aop:aspect> </aop:config> ……其他配置 </beans>

 


<aop:pointcut>标签的expression属性使用前面介绍的切入点表达式语言,也就是说支持AspectJ切入点表达式。但是由于xml对"&&"、"||"、"!"等逻辑运算符不友好,@AspectJ切入点表达式语言中使用的这些逻辑运算符在xml配置中需要分别用"and"、"or"和"not"来代替。
有时候,我们也需要在xml中使用@Pointcut注解声明的切入点,那么该如何呢?大家可能记得,我们可以在切入点表达式中可以引用另一个切入点。对了,就在这里,我们使用该特性可以完成这个任务,如下:

 

Xml代码

<aop:pointcut id="test" expression="aop.test.MyAspect.Pointcut1()" />

 

注意:这里我们必须使用全路径来标示引用的切入点。

4、 声明一个通知
      和@AspectJ一样,基于AOP命名空间的配置也可以定义五种通知类型,并且使用方式和特性类似。与@AspectJ不同的是,配置信息从Annotation中转移到了xml配置文件。
    1)、前置通知
    声明一个前置通知可以使用<aop:aspect>的子标签<aop:before/>。该标签的属性说明如下表:

<aop:before/>标签属性说明

 

属性 <o:p> </o:p>

说明 <o:p> </o:p>

pointcut<o:p></o:p>

指定该通知的内置切入点 <o:p> </o:p>

pointcut-ref<o:p></o:p>

通过 id 引用已定义的切入点 <o:p> </o:p>

method<o:p></o:p>

指定通知对应的方法,该方法必须已在切面的 backing bean 中被声明 <o:p> </o:p>

arg-names<o:p></o:p>

通过方法的参数名字来匹配切入点参数 <o:p> </o:p>

      对于一个通知来说,切入点和对应的通知方法是必须的。也就是说,在这些属性中,method属性是必须的,我们必须要给通知指定一个对应的方法;pointcut属性和pointcut-ref必须有一个被指定,以此确定该通知的切入点。范例如下:

Xml代码

<aop:aspect ref="MyAspect" order="0" id="Test"> <aop:pointcut id="testPointcut" expression="execution(* aop.test.TestBean.*(..))"/> <aop:before pointcut-ref="testPointcut" method="beforeAdvice"/> </aop:aspect>

 

     2)、 后置通知
     声明一个后置通知使用<aop:after/>标签,它的属性等和<aop:before/>标签类似,下面是范例:

Xml代码

<aop:aspect ref="MyAspect" order="0" id="Test"> <aop:pointcut id="testPointcut" expression="execution(* aop.test.TestBean.*(..))" /> <aop:after pointcut-ref="testPointcut" method="AfterAdvice"/> </aop:aspect>

 

     3)、 返回后通知
      <aop:after-returning/>标签可以声明一个返回后通知,该标签的属性和<aop:before/>相比它多了一个returning属性。该属性的意义类似于@AfterReturning注解的returning属性,用于将链接点的返回值传给通知方法。用法如下:

Xml代码
  1.   

<aop:aspect ref="MyAspect" order="0" id="Test"> <aop:pointcut id="testPointcut" expression="execution(* aop.test.TestBean.*(..))" /> <aop:after-returning pointcut-ref="testPointcut" method="AfterReturnAdvice" returning="reVlue" /> </aop:aspect>

 

       4)、 异常通知
        声明一个异常通知使用<aop:after-throwing />标签,它有一个类似于throwing属性又来指定该通知匹配的异常类型。用法如下:

Xml代码

<aop:aspect ref="MyAspect" order="0" id="Test"> <aop:pointcut id="testPointcut" expression="execution(* aop.test.TestBean.*(..))" /> <aop:after-throwing pointcut-ref="testPointcut" method="afterThrowingAdvice" throwing="throwable" /> </aop:aspect>

 

      5)、 环绕通知
      环绕通知是所有通知中功能最强大的通知,用<aop:around/>标签来声明。用法如下:

Xml代码
  1. <aop:aspect ref="MyAspect" order="0" id="Test">  
  2.     <aop:pointcut id="testPointcut"  
  3.         expression="execution(* aop.test.TestBean.*(..))" />  
  4.         <aop:around pointcut-ref="testPointcut" method="aroundAdvice"/>  
  5. </aop:aspect>  

你可能感兴趣的:(AOP的一些东西(事务的传播特性,前置,后置,环绕,异常 等各种通知))