Spring学习笔记(二)

##6.Spring transaction

   1.spring 提供对事务的支持,可以是编程式事务如使用transactionTemplate或使用声明式事务,在传统的声明式事务是使用TransactionProxyFactoryBean进行代理dao.如:

   <bean id="personDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
         <!--   为事务代理bean注入事务管理器-->
        <property name="transactionManager"><ref bean="transactionManager"/></property>
        <!--   设置事务属性-->
        <property name="transactionAttributes">
              <props>
                    <!--   所有以find开头的方法,采用required的事务策略,并且只读-->
                    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                    <!--   其他方法,采用required的事务策略 ->
                     <prop key="*">PROPAGATION_REQUIRED</prop>
              </props>
         </property>
         <!--   为事务代理bean设置目标bean -->
        <property name="target">
               <!--   采用嵌套bean配置目标bean-->
               <bean class="lee.PersonDaoHibernate">
                  <!--   为DAO bean注入SessionFactory引用-->
                  <property name="sessionFactory"><ref local="sessionFactory"/></property>
               </bean>
        </property>
</bean>
2.spring3 后使用tx命名和·@annotaction来声明事务

###使用xml声明事务的过程:

  首先声明HibernateTransactionManager的bean,然后通过<tx:Advice 标签设置transactionManager的属性,最后通过<aop:Advisor> 来指出在哪些链接点上应用这个方面(aspect),可以在多个连接点应用,如只要实现了该testDao便要应用这个transaction aspect.

   <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="*" propagation="REQUIRED" read-only="true"/>
        </tx:attributes>
       </tx:advice>

<aop:advisor advice-ref="txAdvice" pointcut="execution(* *..ItestDao.*(..))"/>

###使用  <tx:annotation-driven transaction-manager="transactionManager"/> 来声明transaction更简单,只需在类或方法上加上annotation @Transactional便可,如:

@Transactional(propagation=Propagation.REQUIRED, readOnly=true)
public interface ItestDao {
   public PMMConfig getPMMConfig(String polno);
}

然后在applicationContext.xml里加上<tx:annotation-driven transaction-manager="transactionManager"/> 即可。

 

 ###Spring MVC

###Spring Security

  1;.spring security使用AOP和servlet filter来实现的。

  2:在web.xml中定义springSecurityFilterChain,当我们配置<http>时,spring会为我们创建那个filter bean。

  3:

 

 

你可能感兴趣的:(spring)