Spring Boot configuration file application.properties

In Spring Boot, the configuration file has two different formats, one is properties and the other is yaml.

Although properties files are more common, yaml is more concise and clearer than properties, and it uses more scenarios. Many open source projects use yaml for configuration (such as Hexo). In addition to conciseness, yaml has another feature, that is, the data in yaml is ordered, and the data in properties is disordered. In some configurations that require path matching, the order is particularly important (for example, we are in Spring Cloud Zuul In the configuration), we generally use yaml at this time. About yaml, Song Ge wrote an article before: Introduction to yaml configuration in Spring Boot .

This article mainly looks at the problem of properties.

Location problem

First of all, when we create a Spring Boot project, there is an application.properties file in the resources directory by default. You can configure the project in the application.properties file, but this file is not the only configuration file. In Spring Boot, there are a total of The application.properties file can be stored in 4 places.

  1. Under the config directory under the current project root directory
  2. The root directory of the current project
  3. Under the config directory under the resources directory
  4. resources directory

According to the above sequence, the priority of the four configuration files decreases in turn. as follows:

These four locations are the default locations, that is, Spring Boot starts. By default, relevant properties will be found and loaded in order from these four locations. However, this is not absolute, we can also customize the configuration file location when the project starts.

For example, now create a javaboy directory under the resources directory and store an application.properties file in the directory. Normally, when we start the Spring Boot project, this configuration file will not be automatically loaded. We can manually specify the location of the configuration file through the spring.config.location property. After the specification is completed, the system will automatically find the application.properties file in the specified directory.

At this time the project starts, it will find, in order to project classpath:/javaboy/application.propertiethe profile start.

This is the startup location configured in the development tool. If the project has been packaged into a jar, just add the location parameter to the startup command:

java -jar properties-0.0.1-SNAPSHOT.jar --spring.config.location=classpath:/javaboy/

File name problem

For application.properties, it does not have to be called application, but the project defaults to load a configuration file named application. If our configuration file is not called application, it is also possible, but we need to specify the configuration file file clearly name.

The method is the same as the specified path, except that the key at this time is spring.config.name.

First, we create an app.properties file in the resources directory, and then specify the file name of the configuration file in IDEA:

After specifying the configuration file name, start the project again, and the system will automatically go to the default four locations to find the configuration file named app.properties. Of course, the configuration files that allow custom file names are not placed in the four default locations, but are placed in a custom directory. At this time, you need to specify spring.config.location explicitly.

The configuration file location and file name can be customized at the same time.

Ordinary attribute injection

Since Spring Boot is derived from Spring, the property injection that exists in Spring also exists in Spring Boot. In Spring Boot, the application.properties file is automatically loaded by default, so simple property injection can be written directly in this configuration file.

For example, now define a Book class:

public class Book {
    private Long id;
    private String name;
    private String author;
    //省略 getter/setter
}

Then, define the properties in the application.properties file:

book.name=三国演义
book.author=罗贯中
book.id=1

In the traditional way (the way in Spring), these attributes can be injected directly into the Book object through the @Value annotation:

@Component
public class Book {
    @Value("${book.id}")
    private Long id;
    @Value("${book.name}")
    private String name;
    @Value("${book.author}")
    private String author;
    //省略getter/setter
}

note

The Book object itself must also be handed over to the Spring container to manage. If the Book is not handed over to the Spring container, then the properties in the Book cannot get values ​​from the Spring container.

After the configuration is complete, inject the Book object into the Controller or unit test, start the project, and you can see that the properties have been injected into the object.

Generally speaking, we mainly store the system configuration in the application.properties file. This kind of custom configuration is not recommended to be placed in this file. You can customize the properties file to have a custom configuration.

For example, in the resources directory, customize the book.properties file with the following content:

book.name=三国演义
book.author=罗贯中
book.id=1

At this point, the configuration file will not be automatically loaded when the project is started. If it is in the XML configuration, the properties file can be referenced as follows:

<context:property-placeholder location="classpath:book.properties"/>

If it is in the Java configuration, you can use @PropertySource to introduce the configuration:

@Component
@PropertySource("classpath:book.properties")
public class Book {
    @Value("${book.id}")
    private Long id;
    @Value("${book.name}")
    private String name;
    @Value("${book.author}")
    private String author;
    //getter/setter
}

In this way, when the project starts, the book.properties file is automatically loaded.

This is just a simple usage of property injection in Spring and has nothing to do with Spring Boot.

Type-safe attribute injection

Spring Boot introduces type-safe property injection. If the configuration method in Spring is adopted, when there are many properties to be configured, the workload is very large and error-prone.

Using type-safe attribute injection can effectively solve this problem.

@Component
@PropertySource("classpath:book.properties")
@ConfigurationProperties(prefix = "book")
public class Book {
    private Long id;
    private String name;
    private String author;
    //省略getter/setter
}

Here, it is mainly to introduce @ConfigurationProperties(prefix = "book") annotation, and configure the prefix of the property. At this time, the corresponding data in the Spring container will be automatically injected into the corresponding property of the object, so there is no need to inject one by one through the @Value annotation , Reduce the workload and avoid errors.

to sum up

application.properties is an important carrier of configuration in Spring Boot, and many component properties can be customized here. Its usage is similar to yaml. For yaml configuration, you can refer to the introduction to yaml configuration in Spring Boot .

I have uploaded the case in this article to GitHub: https://github.com/lenve/javaboy-code-samples

Guess you like

Origin blog.csdn.net/xulong5000/article/details/107409365