如题所述
第ä¸ç§ï¼æ¯ä¸ªBeané½æä¸ä¸ªä»£çï¼
<?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-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- æ°æ®æº -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="root" />
<!-- è¿æ¥æ± å¯å¨æ¶çåå§å¼ -->
<property name="initialSize" value="10" />
<!-- è¿æ¥æ± çæå¤§å¼ -->
<property name="maxActive" value="10" />
<!-- æ大空é²å¼.å½ç»è¿ä¸ä¸ªé«å³°æ¶é´åï¼è¿æ¥æ± å¯ä»¥æ ¢æ ¢å°å·²ç»ç¨ä¸å°çè¿æ¥æ ¢æ ¢éæ¾ä¸é¨åï¼ä¸ç´åå°å°maxIdleä¸ºæ¢ -->
<property name="maxIdle" value="20" />
<!-- æå°ç©ºé²å¼.å½ç©ºé²çè¿æ¥æ°å°äºéå¼æ¶ï¼è¿æ¥æ± å°±ä¼é¢ç³è¯·å»ä¸äºè¿æ¥ï¼ä»¥å 洪峰æ¥æ¶æ¥ä¸åç³è¯· -->
<property name="minIdle" value="10" />
<property name="defaultAutoCommit" value="true" />
</bean>
<!-- ä¼è¯å·¥å -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations">
<list>
<value>classpath:/com/nms/entity/**/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect </prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<!-- å®ä¹äºå¡ç®¡çå¨ -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- é ç½®æå¡å± -->
<bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- é ç½®äºå¡ç®¡çå¨ -->
<property name="transactionManager" ref="transactionManager" />
<property name="target" ref="userDaoAgency" />
<property name="proxyInterfaces" value="com.dao.UserDao" />
<!-- é ç½®äºå¡å±æ§ -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
第äºç§ï¼ææBeanå ±äº«ä¸ä¸ªä»£ç
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<!-- æ°æ®æº -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- // é ç½®åä¸ -->
</bean>
<!-- ä¼è¯å·¥å -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- // é ç½®åä¸ -->
</bean>
<!-- å®ä¹äºå¡ç®¡çå¨ -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- å®ä¹äºå¡ -->
<bean id="base" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
<!-- é ç½®äºå¡ç®¡çå¨ -->
<property name="transactionManager" ref="transactionManager" />
<!-- é ç½®äºå¡å±æ§ -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- é ç½®æå¡å± -->
<bean id="userDao" class="com.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 代ç对象 -->
<bean id="userDaoAgency" parent="base">
<property name="target" ref="userDao" />
</bean>
</beans>
第ä¸ç§ï¼æ¦æªå¨ï¼
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<!-- æ°æ®æº -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- // é ç½®åä¸ -->
</bean>
<!-- ä¼è¯å·¥å -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- // é ç½®åä¸ -->
</bean>
<!-- å®ä¹äºå¡ç®¡çå¨ï¼å£°æå¼çäºå¡ï¼ -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- å®ä¹äºå¡ -->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<!-- é ç½®äºå¡å±æ§ -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*DaoImpl</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!-- é ç½®æå¡å± -->
<bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
第åç§ï¼ä½¿ç¨txæ ç¾é ç½®çæ¦æªå¨ï¼
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<!-- æ°æ®æº -->
<bean id="dataSource">
<!-- // é ç½®åä¸ -->
</bean>
<!-- ä¼è¯å·¥å -->
<bean id="sessionFactory">
<!-- // é ç½®åä¸ -->
</bean>
<context:annotation-config />
<context:component-scan base-package="com.dao" />
<!-- å®ä¹äºå¡ç®¡çå¨ -->
<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" />
</tx:attributes>
</tx:advice>
<!-- å®ä¹åé¢ -->
<aop:config>
<aop:pointcut id="interceptorPointCuts" expression="execution(* com.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
</aop:config>
</beans>
第äºç§ï¼æ³¨è§£ï¼
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<!-- æ°æ®æº -->
<bean id="dataSource">
<!-- // é ç½®åä¸ -->
</bean>
<!-- ä¼è¯å·¥å -->
<bean id="sessionFactory">
<!-- // é ç½®åä¸ -->
</bean>
<context:annotation-config />
<!-- 使ç¨æ³¨è§£çå è·¯å¾ -->
<context:component-scan base-package="com.dao" />
<!-- æ¯æ @Transactional æ è®° -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- å®ä¹äºå¡ç®¡çå¨ -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
å¦æ使ç¨äºæ³¨è§£ï¼é£ä¹å®ç°ç±»åºè¯¥è¿æ ·åï¼
@Transactional
@Component("userDaoAgency")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
/**
* 为æ¹æ³å¢å äºå¡å¤çç¹æ§
*/
@Transactional(readOnly=true)
public void getUser(){
}
}
<?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-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- æ°æ®æº -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="root" />
<!-- è¿æ¥æ± å¯å¨æ¶çåå§å¼ -->
<property name="initialSize" value="10" />
<!-- è¿æ¥æ± çæå¤§å¼ -->
<property name="maxActive" value="10" />
<!-- æ大空é²å¼.å½ç»è¿ä¸ä¸ªé«å³°æ¶é´åï¼è¿æ¥æ± å¯ä»¥æ ¢æ ¢å°å·²ç»ç¨ä¸å°çè¿æ¥æ ¢æ ¢éæ¾ä¸é¨åï¼ä¸ç´åå°å°maxIdleä¸ºæ¢ -->
<property name="maxIdle" value="20" />
<!-- æå°ç©ºé²å¼.å½ç©ºé²çè¿æ¥æ°å°äºéå¼æ¶ï¼è¿æ¥æ± å°±ä¼é¢ç³è¯·å»ä¸äºè¿æ¥ï¼ä»¥å 洪峰æ¥æ¶æ¥ä¸åç³è¯· -->
<property name="minIdle" value="10" />
<property name="defaultAutoCommit" value="true" />
</bean>
<!-- ä¼è¯å·¥å -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations">
<list>
<value>classpath:/com/nms/entity/**/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect </prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<!-- å®ä¹äºå¡ç®¡çå¨ -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- é ç½®æå¡å± -->
<bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- é ç½®äºå¡ç®¡çå¨ -->
<property name="transactionManager" ref="transactionManager" />
<property name="target" ref="userDaoAgency" />
<property name="proxyInterfaces" value="com.dao.UserDao" />
<!-- é ç½®äºå¡å±æ§ -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
第äºç§ï¼ææBeanå ±äº«ä¸ä¸ªä»£ç
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<!-- æ°æ®æº -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- // é ç½®åä¸ -->
</bean>
<!-- ä¼è¯å·¥å -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- // é ç½®åä¸ -->
</bean>
<!-- å®ä¹äºå¡ç®¡çå¨ -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- å®ä¹äºå¡ -->
<bean id="base" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
<!-- é ç½®äºå¡ç®¡çå¨ -->
<property name="transactionManager" ref="transactionManager" />
<!-- é ç½®äºå¡å±æ§ -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- é ç½®æå¡å± -->
<bean id="userDao" class="com.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 代ç对象 -->
<bean id="userDaoAgency" parent="base">
<property name="target" ref="userDao" />
</bean>
</beans>
第ä¸ç§ï¼æ¦æªå¨ï¼
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<!-- æ°æ®æº -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- // é ç½®åä¸ -->
</bean>
<!-- ä¼è¯å·¥å -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- // é ç½®åä¸ -->
</bean>
<!-- å®ä¹äºå¡ç®¡çå¨ï¼å£°æå¼çäºå¡ï¼ -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- å®ä¹äºå¡ -->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<!-- é ç½®äºå¡å±æ§ -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*DaoImpl</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!-- é ç½®æå¡å± -->
<bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
第åç§ï¼ä½¿ç¨txæ ç¾é ç½®çæ¦æªå¨ï¼
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<!-- æ°æ®æº -->
<bean id="dataSource">
<!-- // é ç½®åä¸ -->
</bean>
<!-- ä¼è¯å·¥å -->
<bean id="sessionFactory">
<!-- // é ç½®åä¸ -->
</bean>
<context:annotation-config />
<context:component-scan base-package="com.dao" />
<!-- å®ä¹äºå¡ç®¡çå¨ -->
<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" />
</tx:attributes>
</tx:advice>
<!-- å®ä¹åé¢ -->
<aop:config>
<aop:pointcut id="interceptorPointCuts" expression="execution(* com.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
</aop:config>
</beans>
第äºç§ï¼æ³¨è§£ï¼
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<!-- æ°æ®æº -->
<bean id="dataSource">
<!-- // é ç½®åä¸ -->
</bean>
<!-- ä¼è¯å·¥å -->
<bean id="sessionFactory">
<!-- // é ç½®åä¸ -->
</bean>
<context:annotation-config />
<!-- 使ç¨æ³¨è§£çå è·¯å¾ -->
<context:component-scan base-package="com.dao" />
<!-- æ¯æ @Transactional æ è®° -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- å®ä¹äºå¡ç®¡çå¨ -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
å¦æ使ç¨äºæ³¨è§£ï¼é£ä¹å®ç°ç±»åºè¯¥è¿æ ·åï¼
@Transactional
@Component("userDaoAgency")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
/**
* 为æ¹æ³å¢å äºå¡å¤çç¹æ§
*/
@Transactional(readOnly=true)
public void getUser(){
}
}
温馨提示:答案为网友推荐,仅供参考