您的位置:首页 > 编程语言 > Java开发

Spring3.0.5和Hibernate3.6.0集成的事务控制配置方法

2014-03-14 14:15 543 查看
以下代码示例主要是说明Spring和Hibernate集成后的事务配置方法及使用Spring的AOP实现方法拦截的配置方法()。

环境要求,Spring3.0.5、Hibernate3.6.0

<?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: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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 定义Proxool数据源 -->
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
<property name="driverUrl" value="jdbc:sqlserver://localhost:1433;databaseName=NewDB;"></property>
<property name="user" value="sa"></property>
<property name="password" value="123456"></property>
<property name="maximumConnectionCount" value="3"></property>
<property name="houseKeepingTestSql" value="select getdate()"></property>
</bean>
<!-- 定义Hibernate sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="destroy">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/zywang/model/Student.hbm.xml</value>
<value>com/zywang/model/Teacher.hbm.xml</value>
</list>
</property>
</bean>
<!-- ===================================事务定义开始================================ -->
<!-- Hibernate事务管理器 -->
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 定义通知拦截方法 -->
<tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
<tx:attributes>
<tx:method name="*" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 事务拦截切面定义 -->
<aop:config>
<aop:pointcut expression="execution(* com.zywang.services.*.*(..))" id="servicesOperation"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicesOperation"/>
</aop:config>
<!-- ===================================事务定义结束================================ -->

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息