Analysis of springboot data source

DataSourceAutoConfiguration analysis


SpringBoot has an automatic configuration DataSourceAutoConfiguration to find the DataSourceAutoConfiguration configuration class for the data source configuration /META-INF/spring.factories file
.

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({
    
     DataSource.class, EmbeddedDatabaseType.class })
@EnableConfigurationProperties(DataSourceProperties.class)
@Import({
    
     DataSourcePoolMetadataProvidersConfiguration.class, DataSourceInitializationConfiguration.class })

Under springboot, only the EmbeddedDatabaseType type is popular, indicating that there is no related dependency introduction, and it belongs to spring-jdbc dependency, then add (copied from spring-boot-starter-data-jdbc)

<dependency>
  <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.1.RELEASE</version>
    <scope>compile</scope>
</dependency>

At this time, restart the project and find that there is already org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration in the container, that is, DataSourceAutoConfiguration takes effect at this time, but it is only the DataSourceAutoConfiguration class that is injected into the container. To automatically configure the data source, EmbeddedDatabaseConfiguration
or PooledDataSourceConfiguration also needs to take effect

When the following dependencies are introduced, spring-jdb can be removed because spring-jdb dependencies are introduced in spring-boot-starter-data-jdbc

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>

DataSourceAutoConfiguration will trigger automatic configuration. Refer to the portal
when the configuration takes effect.

Digression The difference between spring-boot-starter-jdbc and spring-boot-starter-data-jdbc
1. You will find that spring-boot-starter-data-jdbc depends on spring-boot-starter-jdbc and spring-data- jdbc
and spring-data-jdbc introduce spring-data-commons, where the CrudRepository interface provides basic addition, deletion, modification and query, and this interface is in spring-data-commons.

2. The default data source of springboot is HikariDataSource, and HikariCP depends on spring-boot-starter-jdbc. Dependencies
used in the above tests

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
    </dependencies>

Test class MyBatisPlusBootMain

@SpringBootApplication
public class MyBatisPlusBootMain implements CommandLineRunner, ApplicationContextAware {
    
    

    Logger logger = LoggerFactory.getLogger(MyBatisPlusBootMain.class);

    @Autowired(required = false)
    DataSource dataSource;

    private  ApplicationContext applicationContext;

    public static void main(String[] args) {
    
    
        ApplicationContext run = SpringApplication.run(MyBatisPlusBootMain.class, args);
        UsersEntity user01 = run.getBean("user01", UsersEntity.class);
        System.out.println(user01);
        DataSourceAutoConfiguration dataSourceAutoConfiguration = run.getBean("org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration", DataSourceAutoConfiguration.class);
        System.out.println(dataSourceAutoConfiguration);
    }

    @Override
    public void run(String... args) throws Exception {
    
    
        System.out.println(">>>>>>>>>>>>>>>>>服务启动执行");
        showConnection();
        showIOCBean();
    }

    private void showIOCBean() {
    
    
        String[] names = this.applicationContext.getBeanDefinitionNames();
        for (int i = 0; i < names.length; i++) {
    
    
            System.out.println(names[i]);
        }
    }

    private void showConnection() throws SQLException {
    
    

        logger.info("dataSource:{}", dataSource.getClass().getName());
        Connection connection = dataSource.getConnection();
        logger.info("connection:{}", connection.toString());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    
    
        this.applicationContext=applicationContext;
    }
}

yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 3306

Replace the default data source with DruidDataSource

Ref: Portal

method one:

add dependencies

<dependency>
          <groupId>com.alibaba</groupId>
           <artifactId>druid</artifactId>
           <version>1.1.23</version>
</dependency>

yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/bms_test
    username: root
    password: 3306
    type: com.alibaba.druid.pool.DruidDataSource

At this point the data source is changed to DruidDataSource

Method Two:

1. Add druid dependency
2. Create the bean of DataSource, and return DruidDataSource after related configuration

@Bean
    public DataSource dataSource() {
    
    
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/bms_test");
        dataSource.setUsername("root");
        dataSource.setPassword("3306");
        return dataSource;
    }

Multiple data source configuration

Guess you like

Origin blog.csdn.net/qq_43566782/article/details/128462597