In Spring Boot, configuration files are available in two different formats, properties and YAML.
While properties files are common, YAML is more concise and uses more scenarios than Properties, and many open source projects use YAML for configuration (e.g., Hexo). In addition to brevity, yamL also features ordered data in YAML and unordered data in properties, which is important in some configurations that require path matching (such as our configuration in Spring Cloud Zuul). Yaml is generally used at this point. Songo previously wrote about YAML: Introduction to YAML Configuration in Spring Boot.
This article focuses on the properties issue.
Location problem
First, when we create a Spring Boot project, by default there is an application.properties file in the Resources directory. But this file is not the only configuration file. There are four places to store the application.properties file in Spring Boot.
- In the config directory of the current project root directory
- The root directory of the current project
- Config directory in the resources directory
- The resources directory
In the preceding order, the priorities of the four configuration files decrease. As follows:
These four locations are the default locations from which Spring Boot starts, and the associated properties are found and loaded in sequence by default. However, this is not absolute, and we can also customize the configuration file location at project startup.
For example, if we now create a Javaboy directory under the Resources directory with an application.properties file in it, normally this configuration file will not be automatically loaded when we start the Spring Boot project. You can manually specify the location of the configuration file using the spring.config.location property. After specifying the location, the system will automatically look for the application.properties file in the specified directory.
Start the project at this time, you will find, the project to the classpath: / javaboy/application. The propertie configuration file.
This is to configure the startup location in the development tool. If the project is packaged as a JAR, add the location parameter to the startup command:
Java jar properties - 0.0.1 - the SNAPSHOT. Jar -- spring. Config. The location = classpath: / javaboy /Copy the code
File name problem
For application.properties, it doesn’t have to be called Application, but the project defaults to loading a configuration file named Application, which is fine if our configuration file isn’t called Application, but, Specify the name of the configuration file.
The method is the same as the specified path except that the key is spring.config.name.
Create app.properties file in resources directory, and then specify the name of the configuration file in IDEA:
After you specify the configuration file name and start the project again, the system will automatically look for the configuration file named app.properties in the four default locations. Of course, configuration files that allow custom filenames are not placed in the four default locations, but in the custom directory, in which case you need to specify spring.config.location explicitly.
Configuration file location and file name can be customized at the same time.
Ordinary property injection
Since Spring Boot is derived from Spring, property injection that exists in Spring also exists in Spring Boot. Since Spring Boot automatically loads the application.properties file by default, simple property injection can be written directly into this configuration file.
For example, now define a Book class:
public class Book {
private Long id;
private String name;
private String author;
/ / omit getter/setter
}
Copy the code
Then, define the properties in the application.properties file:
Book. name= Romance of The Three Kingdoms book.author= Luo Guanzhong book.id=1Copy the code
In the traditional way (as in Spring), you can inject these attributes directly into the Book object via 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;
/ / omit getter/setter
}
Copy the code
Pay attention to
The Book object itself is handled by the Spring container. If Book is not handled by the Spring container, properties in Book cannot get values from the Spring container.
After the configuration is complete, inject the Book object in the Controller or unit test, start the project, and see that the properties have been injected into the object.
In general, we mainly store system configurations in the application.properties file. This custom configuration is not recommended in this file. You can customize the properties file to have custom configurations.
For example, in the Resources directory, customize the book.properties file as follows:
Book. name= Romance of The Three Kingdoms book.author= Luo Guanzhong book.id=1Copy the code
The configuration file is not automatically loaded when the project is started. If you are in an XML configuration, you can reference the properties file as follows:
<context:property-placeholder location="classpath:book.properties"/>
Copy the code
If you are in a Java configuration, you can introduce the configuration via @propertysource:
@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
}
Copy the code
This will automatically load the book.properties file when the project starts.
This is a simple use of property injection in Spring and has nothing to do with Spring Boot.
Type-safe property injection
Spring Boot introduces type-safe attribute injection. If you use the configuration method in Spring, when configuring a large number of attributes, the workload is very heavy, 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;
/ / omit getter/setter
}
Copy the code
The @configurationProperties (prefix = “book”) annotation will automatically inject the corresponding data in the Spring container into the corresponding properties of the object, instead of using the @Value annotation one by one. Reduce workload and avoid mistakes.
conclusion
Application.properties is an important vehicle for configuration in Spring Boot, where many component properties can be customized. The usage of YAML is similar to that of YAML. For details about YAML configuration, see the Introduction to YAML Configuration in Spring Boot.
This case I have uploaded to GitHub: github.com/lenve/javab…
All right, if you have any questions, please leave a comment.
Pay attention to the public account [Jiangnan little Rain], focus on Spring Boot+ micro service and front and back end separation and other full stack technology, regular video tutorial sharing, after attention to reply to Java, get Songko for you carefully prepared Java dry goods!