随笔-348  评论-598  文章-0  trackbacks-0
     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
          
<property name="sessionFactory">
               
<ref local="sessionFactory" />
          
</property>
     
</bean>
         
    
<bean id="ordersDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
         
<property name="transactionManager">
              
<ref bean="transactionManager" />
         
</property>
         
<property name="target">
              
<ref local="ordersDAO" />
         
</property>
         
<property name="transactionAttributes">
              
<props>
                   
<prop key="insert*">PROPAGATION_REQUIRED</prop>
                   
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                   
<prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
              
</props>
         
</property>
    
</bean>
    
    
<bean id="ordersDAO" class="com.gcoresoft.hibernate.OrdersDAO">
        
<property name="sessionFactory">
            
<ref local="sessionFactory" />
        
</property>
    
</bean>

    
<bean name="/addOrder" class="com.gcoresoft.struts.action.OrderAction" singleton="false">
         
<property name="ordersDAO">
              
<ref local="ordersDAOProxy" />
         
</property>
    
</bean>
这个是传统的事务代理方式,下面的是目前开发中一种常用的事务代理方式(感谢Amigo姐姐)
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      
<property name="sessionFactory">
       
<ref local="sessionFactory" />
      
</property>
     
</bean>

    
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
        
<!--  事务拦截器bean需要依赖注入一个事务管理器 -->
        
<property name="transactionManager" ref="transactionManager" />
        
<property name="transactionAttributes">
            
<!--  下面定义事务传播属性-->
            
<props>
                
<prop key="insert*">PROPAGATION_REQUIRED</prop>
                   
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                   
<prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
            
</props>
        
</property>
    
</bean>

    
<!-- 定义BeanNameAutoProxyCreator-->
    
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        
<!--  指定对满足哪些bean name的bean自动生成业务代理 -->
        
<property name="beanNames">
            
<!--  下面是所有需要自动创建事务代理的bean-->
            
<list>
                
<value>ordersDAO</value>
            
</list>
            
<!--  此处可增加其他需要自动创建事务代理的bean-->
        
</property>
        
<!--  下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
        
<property name="interceptorNames">
            
<list>
                
<!-- 此处可增加其他新的Interceptor -->
                
<value>transactionInterceptor</value>
            
</list>
        
</property>
    
</bean>

    
<bean name="/addOrder" class="com.gcoresoft.struts.action.OrderAction" singleton="false">
     
<property name="ordersDAO">
      
<ref bean="ordersDAO" />
     
</property>
    
</bean>
不过,如果想使用代理,OrderDAO就必须是一个接口的实现,public class OrdersDAO extends HibernateDaoSupport implements IOrdersDAO ,然后在相应的OrderAction里面
    private IOrdersDAO ordersDAO;
    
    
/**
     * 
@param ordersDAO
     
*/

    
public void setOrdersDAO(IOrdersDAO ordersDAO)
    
{
        
this.ordersDAO=ordersDAO;
    }
这样才能使用事务代理,而不能直接将OrdersDAO作为传递参数的类型,因为那样程序会报错
Error creating bean with name '/addOrder' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is PropertyAccessExceptionsException (1 errors)。

P.S:MyEclipse里面可以使用Hibernate Reverse Engineering来生成DAO,然后用Eclipse的Refactor来Extract Interface,这样就方便多了。呵呵!

---------------------------------------------------------
专注移动开发

Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2007-05-10 22:27 TiGERTiAN 阅读(655) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: