Home > Java > javaTutorial > How to implement default and custom configuration files with Spring Boot

How to implement default and custom configuration files with Spring Boot

WBOY
Release: 2023-06-23 09:53:08
Original
1890 people have browsed it

In the actual development process, we often need to use configuration files in the project. Spring Boot is a popular framework in which we can configure the behavior of the application using default configuration files or custom configuration files. This article will introduce how to use Spring Boot’s default and custom configuration files.

1. Default configuration files

Spring Boot provides many default configuration files, which are located in the src/main/resources directory. If we do not specify the name of any configuration file, Spring Boot will automatically use application.properties or application.yml as the default configuration file. In the default configuration file, we can define various properties and values ​​to configure the application's behavior. Below is an example of a simple application.properties file.

# 数据库连接配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=123456

# 日志配置
logging.level.org.springframework=debug
logging.level.com.acme=trace
Copy after login

In the above example, we configured the relevant properties for connecting to the MySQL database, as well as the log level configuration. It is very convenient to use the default configuration file, but in some cases, we may need to use a custom configuration file.

2. Customized configuration files

We can create multiple customized configuration files to use different configuration files in different environments. Custom configuration files can be placed anywhere, just make sure you specify the correct configuration file location when your application starts. Here's how to specify the location of a custom configuration file.

First, create a file named myconfig.properties, which contains some custom properties.

# 自定义属性
server.port=8081
app.version=1.2.0
app.name=My Application
Copy after login

Then, we need to specify the name of the configuration file that needs to be loaded in the startup class of the application, and use the @PropertySource annotation to import the specified configuration file.

@SpringBootApplication
@PropertySource("classpath:myconfig.properties")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Copy after login

In the above example, we used the @PropertySource annotation to specify the loading of the myconfig.properties configuration file. In addition, we can also combine multiple configuration files together, as shown below:

@SpringBootApplication
@PropertySources({
        @PropertySource("classpath:application.yml"),
        @PropertySource("classpath:myconfig.properties")
})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Copy after login

Here we use the @PropertySources annotation to load two configuration files, namely application.yml and myconfig.properties.

3. Use custom attributes

Using custom attributes in an application is very simple. Just use the @Value annotation on a class or method to inject the attribute value into the corresponding in variables. The following is the sample code:

@RestController
public class MyController {
    @Value("${app.name}")
    private String appName;

    @RequestMapping("/")
    public String home() {
        return "Hello, " + appName;
    }
}
Copy after login

In the above example, we used the @Value annotation to inject the value of the app.name property in the myconfig.properties file into the appName variable. When accessing the application home page, the message Hello, My Application will be displayed.

Summary

This article introduces how to use Spring Boot's default configuration files and custom configuration files to configure the behavior of the application, and how to use custom properties in the application. In actual projects, we often need to use different configuration files according to different environments. In this case, we need to use custom configuration files. Spring Boot provides many convenient tools to help us use default and custom configuration files to make our applications more flexible and adaptable to the needs of different environments.

The above is the detailed content of How to implement default and custom configuration files with Spring Boot. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template