[Project combat] SpringBoot configuration file

1. Spring Boot configuration file

The Spring Boot project uses a global configuration file.
The Spring Boot configuration file name is fixed application.properties or application.yml.
SpringBoot provides a variety of configuration files, such as .properties, .yaml, .json and other configuration files.
In the SpringBoot project, you can configure some property values ​​of the project through the configuration file, such as database connection, mail server, etc.

The configuration files of the Spring Boot project are stored in the resources directory by default.
In fact, when the Spring Boot system starts, it will read configuration files in four different paths, and the priority of the application.properties files in these four locations will decrease in the order listed above.

Two, two Spring Boot configuration file names

2.1 application.properties

This is a plain text file that configures Spring Boot properties.
The purpose of this file is to modify the default values ​​​​of Spring Boot's automatic configuration.

2.2 application.yml

This is a YAML file and also properties used to configure Spring Boot.

3. Simple SpringBoot configuration file example

The following is an example of a simple SpringBoot configuration file:

3.1 Creation of configuration files

You can create a configuration file in the src/main/resources directory, such as test.properties, test.yaml, test.json and other formats. The following is an example of a configuration file in .properties format:

spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.max-idle=10
spring.datasource.min-idle=5
spring.datasource.initial-size=5
spring.datasource.time-between-eviction-runs-millis=60000
spring.datasource.max-lifetime=3600000
spring.datasource.validation-query=SELECT 1

3.2 Reading of configuration files

In SpringBoot, the attribute values ​​​​of the configuration file can be read through the @ConfigurationProperties annotation and injected into the class. Here is a simple example:

@Configuration
public class AppConfig {
    
    
    @Autowired
    private DataSource dataSource;
    @Bean
    public DataSource dataSource() {
    
    
        return DataSourceBuilder.create().build();
    }
}

In the above code, the property values ​​in the test.properties configuration file are read through the @ConfigurationProperties annotation and injected into the dataSource. At the same time, a dataSource() method is defined to create a data source object.

3.3 Use of configuration files

In SpringBoot, the property values ​​in the configuration file can be autowired by using the @Autowired annotation in the class.
Here is a simple example:

@Service
public class UserServiceImpl implements UserService {
    
    
    @Autowired
    private UserDao userDao;
    @Autowired
    private DataSource dataSource;
    @Override
    public void addUser(User user) {
    
    
        userDao.addUser(user);
    }
}

In the above code, the @Autowired annotation is used to automatically assemble the UserDao and DataSource objects, and use them to implement the function of adding users.

Guess you like

Origin blog.csdn.net/wstever/article/details/129927087