SpringBoot notes - detailed explanation of the relevant configuration in Spring

SpringBoot is adopted by more and more microservice practitioners because of its lightweight, embedded web container, one-key startup, and convenient debugging.

Today's blog is also a study note for springboot, mainly talking about related configurations

Related springboot learning videos

https://www.bilibili.com/video/BV1XQ4y1m7ex

Related configuration in Spring

1. Web project in Spring

First, a main startup class is required as the entry point of the entire springboot web project

package com.fan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//这是springboot的一个启动器,启动之后就能生产出来一个spring整合的web应用
//注意后面引用的相关组件不能脱离这个启动器所在的包,比如说Controller类
//启动的时候会看见内置的tomcat进行启动,还会通过DispatcherServlet进行映射spring-mvc的Controller
//把xml的配置放到的spring中全注解的配置中
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

2. Create a Druid connection pool and perform attribute binding for it

First create a property reading class: It should be noted that the properties of our SpringBoot will read a configuration file named application.properties under Resources by default, and specify the properties to be read in the property file of this class The prefix needs to be annotated on the class: @ConfigurationProperties (prefix = "jdbc") can be read:

For example:

jdbc.uername= .....

jdbc.password=..... and other related properties

package com.fan.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
//表示当前类是属性读取类,读取属性文件中的前缀是jdbc,来读取指定的属性
//我们的springboot会默认读取一个名为:application。properties这个属性文件中的属性
@ConfigurationProperties(prefix = "jdbc")
public class JdbcProperties {
    private String driverClassName;
    private String url;
    private String username;
    private String password;

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

configuration file:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/springboot
jdbc.username=root
jdbc.password=123456

3. Need to write a configuration file to inject the relevant Value in the DataSource connection pool

Note: @Configuration declares that this class is a configuration class

@EnableConfiguration (jdbcproperties.class) is equivalent to creating a property reading class in the spring container, that is, handing over the creation method of the property reading class to the spring container for management. When we inject dataSource below, we can directly use @Autowired to get it to the instance object

package com.fan.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

//对应的相当于一个配置文件
@Configuration
/**
 * 对这个类进行解读:
 * 1.@Configuration:声明我们jdbcConfig是一个配置类
 * 2.@PropertySource:指定属性文件的路径是ClassPath:jdbc.properties
 * 3.@Value:为属性注入值
 * 4.@Bean:将DataSource()方法声明为一个注册Bean的方法,Spring会自动调用该方法,将返回值加入Spring容器中。默认的对象就可以在热议位置通过@Autowired进行注入DataSource了
 *
 */
@EnableConfigurationProperties(JdbcProperties.class)//相当于在spring容器中创建属性读取类
public class JdbcConfig {

    @Bean
    public DataSource dataSource(@Autowired JdbcProperties jdbc){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(jdbc.getDriverClassName());
        dataSource.setUrl(jdbc.getUrl());
        dataSource.setUsername(jdbc.getUsername());
        dataSource.setPassword(jdbc.getPassword());

        return dataSource;
    }
}

4. A more elegant injection method

//      通过@COnfigurationproperties这个注解,可以将配置文件中前缀是jdbc的属性自动赋值给dataSource中同名的属性当中,一种黑科技的感觉
@Configuration
public class JdbcConfig{
    @Bean
    @ConfigurationProperties(prefix = "jdbc")
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        return dataSource;
    }
}

Guess you like

Origin blog.csdn.net/weixin_54585403/article/details/123504928
Recommended