搜索 K
Appearance
博客正在加载中...
Appearance
我们在上一篇博客的基础上,继续使用注解来改造案例
jdbcConfig.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql:///learnSpring
jdbc.username=learnSpringUser
jdbc.password=learnSpringPassword
package com.peterjxl.config;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@PropertySource("classpath:jdbcConfig.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;
/**
* 创建 JdbcTemplate 对象
* @param dataSource
* @return
*/
@Bean(name = "jdbcTemplate")
public JdbcTemplate createJdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean(name = "dataSource")
public DataSource createDataSource(){
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
}
注意我们加了 @EnableTransactionManagement 注解,表明开启事务控制
package com.peterjxl.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan("com.peterjxl")
@Import(JdbcConfig.class)
@EnableTransactionManagement
public class SpringConfiguration {
}
以下配置可以删掉:
<context:component-scan base-package="com.peterjxl"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/learnSpring"/>
<property name="username" value="learnSpringUser"/>
<property name="password" value="learnSpringPassword"/>
</bean>
<!-- 开启Spring对注解事务的支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>我们新建一个类,用来返回事务管理的 bean:
package com.peterjxl.config;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
public class TransactionConfig {
@Bean("transactionManager")
public PlatformTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
} 并且, 主配置类也要加上该类的 class 对象(第 3 行):
@Configuration
@ComponentScan("com.peterjxl")
@Import({JdbcConfig.class,TransactionConfig.class})
@EnableTransactionManagement
public class SpringConfiguration {
}此时,我们就可以删除 bean.xml 如下配置了:
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>此时整个 bean.xml 都是空的了,可以删掉。
在配置类中,不使用配置文件了,而是改为传入 class 对象:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest { 然后我们测试,可以看到也是支持事务运行的。
本项目已将源码上传到 GitHub 和 Gitee 上。并且创建了分支 demo20,读者可以通过切换分支来查看本文的示例代码