标签:int ioc creat pack 注入 DDM ram add 根据
(1)创建配置类代替配置文件
package com.orzjiangxiaoyu.spring.util; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidPooledConnection; import org.junit.Test; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; import java.sql.SQLException; /** * @author orz * @create 2020-08-18 22:50 */ @Configuration//配置类 @ComponentScan(basePackages = "com.orzjiangxiaoyu")//组件扫描 @EnableTransactionManagement//开启事务 public class TxConfig { //创建数据库连接池 @Bean public DruidDataSource getDruidDataSource() { DruidDataSource dataSource=new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode\\=true&characterEncoding\\=utf-8"); dataSource.setUsername("root"); dataSource.setPassword("root"); return dataSource; } //创建JdbcTemplate对象 @Bean public JdbcTemplate getJdbcTemplate(DataSource dataSource) { //到ioc中根据类型找到dataSource JdbcTemplate jdbcTemplate=new JdbcTemplate(); //注入dataSoure jdbcTemplate.setDataSource(dataSource); return jdbcTemplate; } //创建事务管理器对象 @Bean public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) { DataSourceTransactionManager dataSourceTransactionManager=new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; } }
package com.orzjiangxiaoyu.spring.service; import com.orzjiangxiaoyu.spring.dao.BankDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; /** * @author orz * @create 2020-08-18 16:32 */ @Service @Transactional public class BankService { @Autowired private BankDao bankDao; //转账方法 //声明式 public void accountMoney() { //少钱操作 bankDao.reduceMoney(); //手动抛出异常 System.out.println(1/0); //多钱操作 bankDao.addMoney(); } }
测试
package com.orzjiangxiaoyu.spring.testdemo; import com.orzjiangxiaoyu.spring.service.BankService; import com.orzjiangxiaoyu.spring.util.TxConfig; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * @author orz * @create 2020-08-18 23:04 */ public class Test2 { @Test public void test1() { ApplicationContext context=new AnnotationConfigApplicationContext(TxConfig.class); BankService bankService = context.getBean("bankService", BankService.class); bankService.accountMoney(); } }
标签:int ioc creat pack 注入 DDM ram add 根据
原文地址:https://www.cnblogs.com/orzjiangxiaoyu/p/13532282.html