Deep and simple introduction of spring boot 2.x configuration data source

Configuring data sources

After relying on spring-boot-starter-data-jpa, it configures data sources for you by default. These default data sources are mainly memory databases, such as h2, hqldb and Derby. Sometimes they need to be configured as the data sources we want.

Start the default data source

Take h2 database as an example, add its dependencies to maven

		<dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

JPA dependency is introduced here. For JPA, spring boot relies on Hibernate to implement, so we can see that there are many Hibernate-related jar s under the maven dependency package.
So we can run the springboot project without using any configuration database, because h2 is an embedded database, it will start with the start of the project without any configuration. But more often than not, we want to use commercial databases such as MySQL and Oracle.

Configure custom data sources

Take MySQL as an example, delete h2 dependencies in maven, retain spring-boot-starter-data-jpa dependencies, and increase MySQL dependencies.

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

Obviously, this is not enough to connect to the data source. It also needs to configure the relevant information of the database to connect to the database.

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
# spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#Maximum number of waiting connections, set 0 to unlimited
spring.datasource.tomcat.max-idle=10
#Maximum number of connection activities
spring.datasource.tomcat.max-active=50

spring.jpa.database = MYSQL
#Maximum number of milliseconds to wait in ms. Error messages will occur over time
spring.datasource.tomcat.max-wait=10000
#Number of database connection pool initialization connections
spring.datasource.tomcat.initial-size=5

In this way, we have completed the data source configuration of spring boot. spring.datasource.url=jdbc:mysql://localhost:3306/test is the database address, followed by zone-time configuration, which will be used in jpa later. This is just a data source that matches Tomcat bound by spring boot. Sometimes we want to use third-party data sources, such as dbcp. We just need to add maven dependencies and configure dbcp2 data sources. We will not show the specific configuration information of DBCP2 here.
Test:

package com.example.JPAdemo;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

//Implementation of spring bean lifecycle interface ApplicationContextAware
@Component
public class DataSourceShow implements ApplicationContextAware {
    ApplicationContext applicationContext = null;
    @Override
    //The spring container automatically calls this method and injects it into the spring IOC container
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        DataSource dataSource =applicationContext.getBean(DataSource.class);
        System.out.println("------------");
        System.out.println(dataSource.getClass().getName());
        System.out.println("------------");
    }
}

In the above code, the method setApplicationContext () of interface ApplicationContextAware is implemented. According to the rules of spring bean life cycle, this method will be called when it is initialized, so as to obtain the context of springioc container, get the database connection pool through getBean, and output day. Zhi:

------------
org.apache.tomcat.jdbc.pool.DataSource
------------

Keywords: Spring MySQL Database Tomcat

Added by Jeroen_nld on Mon, 29 Jul 2019 09:26:23 +0300