首页 > 代码库 > TransactionProxyFactoryBean 配置问题

TransactionProxyFactoryBean 配置问题

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.*" />

    <bean id="sessionFactory"  
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        <property name="configLocation" value="http://www.mamicode.com/classpath:hibernate.cfg.xml" />  
        <property name="configurationClass" value="http://www.mamicode.com/org.hibernate.cfg.AnnotationConfiguration" />
    </bean>  

    <!-- 定义事务管理器(声明式的事务) -->  
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <!-- 配置DAO -->
    <bean id="generatorDaoTarget" class="com.*.spring.dao.GeneratorDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="generatorDao"  
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
        <!-- 配置事务管理器 -->  
        <property name="transactionManager"><ref bean="transactionManager" /></property>  
        <property name="target"><ref bean="generatorDaoTarget" /></property>  
        <property name="proxyInterfaces"><value>com.*.spring.dao.GeneratorDao</value></property>
        <!-- 配置事务属性 -->  
        <property name="transactionAttributes">  
            <props>  
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>  
        </property>  
    </bean>  

    <bean id="plantDaoTarget" class="com.*.spring.dao.PlantDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="plantDao"  
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
           <!-- 配置事务管理器 -->  
        <property name="transactionManager"><ref bean="transactionManager" /></property>     
        <property name="target"><ref bean="plantDaoTarget" /></property> 
        <property name="proxyInterfaces"><value>com.*.spring.dao.PlantDao</value></property>           
        <!-- 配置事务属性 -->  
        <property name="transactionAttributes">  
            <props>  
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>  
        </property>  
    </bean>
    
    <!-- 配置Service -->
    <bean id="plantGeneratorServiceTarget"  
        class="com.*.spring.service.PlantGeneratorServiceImpl">  
        <property name="plantDao">  
            <ref bean="plantDao" />  
        </property>  
        <property name="generatorDao">  
            <ref bean="generatorDao" />  
        </property>  
    </bean>        
    
    <bean id="plantGeneratorService"  
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
           <!-- 配置事务管理器 -->  
           <property name="transactionManager"><ref bean="transactionManager" /></property>     
        <property name="target"><ref bean="plantGeneratorServiceTarget" /></property>  
        <property name="proxyInterfaces"><value>com.*.spring.service.PlantGeneratorService</value></property>
        <!-- 配置事务属性 -->  
        <property name="transactionAttributes">  
            <props>  
                <prop key="*">PROPAGATION_REQUIRED</prop>  
            </props>  
        </property>  
    </bean>  
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

    <!-- 各属性的配置-->
    <!-- 为true表示将Hibernate发送给数据库的sql显示出来 -->
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">none</property> 

    <!-- SQL方言,这边设定的是MySQL -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <!--连接数据库的Driver-->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <!--数据库连接url-->
    <property name="connection.url">jdbc:mysql://localhost:3306/test</property>

    <!--用户名-->
    <property name="connection.username">root</property>
    <!--密码-->
    <property name="connection.password">123456</property>

    <!-- 映射文件  -->
    <mapping class="com.*.spring.domain.Generator" />
    <mapping class="com.*.spring.domain.Plant" />
</session-factory>
</hibernate-configuration>
public interface GeneratorDao {

    /**
     * 获取所有机组数据
     * @return
     */
    public List<Generator> listGenerators();
    
    /**
     * 保存机组数据
     * @param generator 机组数据
     */
    public void save(Generator generator);    
}
public class GeneratorDaoImpl extends HibernateDaoSupport implements GeneratorDao {
       
    @SuppressWarnings("unchecked")
    public List<Generator> listGenerators() {
        return this.getSession().createQuery("from Generator").list();
    }

    public void save(Generator generator) {
        this.getSession().save(generator);    
    }
}