spring进行声明式事务管理(xml)(推荐,可以同时配置好多方法)
package com.test.service;import javax.annotation.Resource;import org.springframework.stereotype.Component;import com.test.dao.LogDAO;import com.test.dao.UserDAO;import com.test.model.Log;import com.test.model.User;@Component("userService")public class UserService {private UserDAO userDAO;private LogDAO logDAO;public void add(User user) {userDAO.save(user);Log log = new Log();log.setMessage("user save success!");logDAO.save(log);}public UserDAO getUserDAO() {return userDAO;}// 这里会找注解Component的值为:userDAOImpl的那个类@Resource(name = "userDAOImpl")public void setUserDAO(UserDAO userDAO) {this.userDAO = userDAO;}public LogDAO getLogDAO() {return logDAO;}@Resource(name = "logDAOImpl")public void setLogDAO(LogDAO logDAO) {this.logDAO = logDAO;}}
<?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:component-scan base-package="com.test"></context:component-scan><bean id="myDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:oralsb"/> <property name="username" value="scott"/> <property name="password" value="tiger"/></bean><!-- annotation版 --><bean id="mySessionFactory" ref="myDataSource"/> <!-- <property name="annotatedClasses"><list> <value>com.test.model.User</value> <value>com.test.model.Log</value></list> </property> --> <!-- 可以使用下面packagesToScan代替上面annotatedClasses --><property name="packagesToScan"><list> <value>com.test.model</value></list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9iDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop><!-- <prop key="hibernate.hbm2ddl.auto">create</prop>--> </props> </property></bean> <bean id="txManager" ref="mySessionFactory" /></bean><!-- 定义建议,使用txManager事务管理者 --><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="add" propagation="REQUIRED"/></tx:attributes></tx:advice><aop:config><!-- 定义一个切入点 --><aop:pointcut id="businessService" expression="execution(public * com.test.service..*.*(..))"/><aop:advisor pointcut-ref="businessService" advice-ref="txAdvice"/></aop:config></beans>
其他内容和“spring进行声明式事务管理(annotation)”这一节一致