学习笔记@玩转Spring全家桶
课程内容来自极客时间玩转Spring全家桶,侵私删,链接如下
https://time.geekbang.org/course/intro/100023501
第二天
Part1如何配置单数据源
StepOne
先通过springboot初始化start.io初始化demo,依赖如下
其中引入对应数据库驱动H2,引入JDBC依赖spring-boot-starter-jdbc
代码如下
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@SpringBootApplication
@Slf4j
public class DataSourceDemoApplication implements CommandLineRunner {
@Autowired
private DataSource dataSource;
@Autowired
private JdbcTemplate jdbcTemplate;
public static void main(String[] args) {
SpringApplication.run(DataSourceDemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
showConnection();
}
private void showConnection() throws SQLException {
log.info(dataSource.toString());
Connection conn = dataSource.getConnection();
log.info(conn.toString());
conn.close();
}
由于SpringBoot自动配置,只需注入DataSource、JdbcTemplate,打印输出info信息
StepTwo
不用springboot需要的配置
datasourcedemo类
package geektime.spring.data.datasourcedemo;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Properties;
@Configuration
@EnableTransactionManagement
public class DataSourceDemo {
@Autowired
private DataSource dataSource;
public static void main(String[] args) throws SQLException {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext*.xml");
showBeans(applicationContext);
dataSourceDemo(applicationContext);
}
@Bean(destroyMethod = "close")
public DataSource dataSource() throws Exception {
Properties properties = new Properties();
properties.setProperty("driverClassName", "org.h2.Driver");
properties.setProperty("url", "jdbc:h2:mem:testdb");
properties.setProperty("username", "sa");
return BasicDataSourceFactory.createDataSource(properties);
}
@Bean
public PlatformTransactionManager transactionManager() throws Exception {
return new DataSourceTransactionManager(dataSource());
}
private static void showBeans(ApplicationContext applicationContext) {
System.out.println(Arrays.toString(applicationContext.getBeanDefinitionNames()));
}
private static void dataSourceDemo(ApplicationContext applicationContext) throws SQLException {
DataSourceDemo demo = applicationContext.getBean("dataSourceDemo", DataSourceDemo.class);
demo.showDataSource();
}
public void showDataSource() throws SQLException {
System.out.println(dataSource.toString());
Connection conn = dataSource.getConnection();
System.out.println(conn.toString());
conn.close();
}
}
applicatioContext.xml
<?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"
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">
<context:component-scan base-package="geektime.spring.data" />
<!--
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:testdb"/>
<property name="username" value="SA"/>
<property name="password" value=""/>
</bean>
-->
</beans>
通过这种方式可以实现跟上述一样的效果
如果DataSource的Bean也不想通过代码来编写的话,可以通过applicatioContext.xml去定义一个Bean,内容是和上述一样的,两者的效果是完全相同的
Part2如何配置多数据源
如果两个数据源
需要把两个数据源的配置分开,所有的配置都需要分开
要清楚当前在操作哪个数据源
在@SpringBootApplication注解中exclude = { DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class,JdbcTemplateAutoConfiguration.class}
排除这三个类(Spring自动配置的类)
//thd相关DataSourceProperties、DataSource、PlatformTransactionManager
@Bean
@ConfigurationProperties("thd.datasource")
public DataSourceProperties thdDataSourceProperties()
{
return new DataSourceProperties();
}
@Bean
public DataSource thdDataSource() {
DataSourceProperties dataSourceProperties = thdDataSourceProperties();
log.info("thd datasource: {}",dataSourceProperties.getUrl());
return dataSourceProperties.initializeDataSourceBuilder().build();
}
@Bean
@Resource
public PlatformTransactionManager thdTxManager(DataSource thdDataSource) {
return new DataSourceTransactionManager(thdDataSource);
}
@Resource用法与@Autowired 用法相似,也是做依赖注入的
区别是byName与byId
https://blog.csdn.net/u010502101/article/details/78950045
@ConfigurationProperties(prefix=" ")
比较常见的就是配置读写分离的场景
该注解有一个prefix属性,通过指定的前缀,绑定配置文件中的配置,该注解可以放在类上,也可以放在方法上
可以从注解说明中看到,当将该注解作用于方法上时,如果想要有效的绑定配置,那么该方法需要有@Bean注解且所属Class需要有@Configuration注解。
https://www.cnblogs.com/tian874540961/p/12146467.html
如果业务逻辑需要三四个数据源的话,操作也是一样的