基於註解配置事務, 只需要三步
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.hr"></context:component-scan>
<context:property-placeholder location="db.properties"></context:property-placeholder>
<!--配置數據源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.Driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--
spring中基於註解的聲明式事務控制配置步驟
1 配置事務管理器
2 開啓spring對註解事務的支持
3 在需要事務支持的地方使用@Transactional註解
-->
<!--1 配置事務管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--2 開啓spring對註解事務的支持-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
業務類:
package com.hr.service.impl;
import com.hr.dao.IAccountDao;
import com.hr.domain.Account;
import com.hr.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service("accountService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true) //只讀型事務的配置
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
public Account findAccountById(Integer accountId) {
Account account = accountDao.findAccountById(accountId);
return account;
}
//需要的是讀寫型事務的配置
@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
public void transfer(String sourceName, String targetName, Float money) {
//查詢轉出賬戶
Account source = accountDao.findAccountByName(sourceName);
//查詢轉入賬戶
Account target = accountDao.findAccountByName(targetName);
//轉出賬戶減錢
source.setMoney(source.getMoney() - money);
//轉入賬戶加錢
target.setMoney(target.getMoney() + money);
//更新轉出賬戶
accountDao.updateAccount(source);
int i = 1 / 0;
//更新轉入賬戶
accountDao.updateAccount(target);
}
}
業務類中配置事務的註解, 可以在類上和方法上分別使用, 區別於只讀型事務和讀寫型事務的配置. 相比於XML配置, 註解在xml配置相對簡單, 但是要區分於類和方法上做不同的處理
github: https://github.com/2402zmybie/spring04_06tx_anno
基於Spring聲明式事務的純註解實現:
package conf;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* Spring配置類, 相當於bean.xml
*/
@Configuration
@ComponentScan("com.hr")
@Import({JdbcConfig.class,TransactionManager.class})
@EnableTransactionManagement //開啓Spring對註解的支持
public class SpringConfiguration {
}
package conf;
import com.mchange.v2.c3p0.DriverManagerDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
* 和連接數據庫相關的配置類
*/
@Configuration
@PropertySource("classpath:db.properties")
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean(name = "jdbcTemplate")
public JdbcTemplate createJdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "dataSource")
public DataSource createDataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClass(driver);
ds.setJdbcUrl(url);
ds.setUser(username);
ds.setPassword(password);
return ds;
}
}
package conf;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* 和事務相關的配置類
*/
public class TransactionManager {
/**
* 用於創建事務管理器對象
* @param dataSource
* @return
*/
@Bean(name = "transactionManager")
public DataSourceTransactionManager createTransactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
github: https://github.com/2402zmybie/spring04_07anno_tx_withoutxml