Spring is the time to figure out the profile of the application.properties Boot!

In Spring Boot, the profile in two different formats, one Properties, and the other is yaml.

Although relatively common file properties, but with respect to the properties concerned, yaml more concise, and the scene also used more, many open source projects are using yaml configured (eg Hexo). In addition to simple, yaml there is another feature that is yaml data is ordered, the data properties are disordered, in need of some path matching configuration, the order is particularly important (for example, we in Spring Cloud Zuul configuration), then we generally use yaml. About before yaml, Song Ge wrote an article: YAML Configuration Introduction Spring Boot in .

In this paper, we take a look at the problem properties.

Location issues

First, when we create a Spring Boot project, there is a default file application.properties resources directory can be configured in application.properties project file, but the file is not the only profile in Spring Boot, a total 4 application.properties place to store files.

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

Order as above, the priority in descending order of the four profiles. as follows:

These four are the default locations, namely Spring Boot startup, by default from four locations in order to find the relevant properties and loads. But this is not absolute, we can also custom profile location when the project started.

For example, now create a directory in the resources javaboy directory, the directory stored in a application.properties file, under normal circumstances, when we start Spring Boot project, this profile is not automatically loaded. We can be manually specified by spring.config.location property profile location, after the specified completion, the system will automatically go to the specified directory to find application.properties file.

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

This is the configuration of the starting position in development tools, if the project has been packaged into a jar, add a location parameter to the startup command:

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

File name issue

For application.properties concerned, it does not have to call application, but the project is to load the default configuration file named application, if we are not called application profiles, is also possible, however, you need to explicitly specify the file configuration file name.

And consistent manner specified path, but this time the key is spring.config.name.

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

After completion of the specified configuration file name, start the project again, the system will automatically default to the following four positions are to find the configuration file named app.properties. Of course, allows custom configuration file file name is not on four default location, but on a custom directory, then you need to explicitly specify spring.config.location.

Configuration file location and name can simultaneously customize.

Common attributes injection

Since the Boot from Spring Spring, so Spring present injection properties, as there are in the Spring of the Boot. Since the Spring Boot, the default file will automatically load application.properties, so simple property injection can be written directly in the configuration file.

For example, the definition of a class Book Now:

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

然后,在 application.properties 文件中定义属性:

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

按照传统的方式(Spring中的方式),可以直接通过 @Value 注解将这些属性注入到 Book 对象中:

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

注意

Book 对象本身也要交给 Spring 容器去管理,如果 Book 没有交给 Spring 容器,那么 Book 中的属性也无法从 Spring 容器中获取到值。

配置完成后,在 Controller 或者单元测试中注入 Book 对象,启动项目,就可以看到属性已经注入到对象中了。

一般来说,我们在 application.properties 文件中主要存放系统配置,这种自定义配置不建议放在该文件中,可以自定义 properties 文件来存在自定义配置。

例如在 resources 目录下,自定义 book.properties 文件,内容如下:

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

此时,项目启动并不会自动的加载该配置文件,如果是在 XML 配置中,可以通过如下方式引用该 properties 文件:

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

如果是在 Java 配置中,可以通过 @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
}

这样,当项目启动时,就会自动加载 book.properties 文件。

这只是 Spring 中属性注入的一个简单用法,和 Spring Boot 没有任何关系。

类型安全的属性注入

Spring Boot 引入了类型安全的属性注入,如果采用 Spring 中的配置方式,当配置的属性非常多的时候,工作量就很大了,而且容易出错。

使用类型安全的属性注入,可以有效的解决这个问题。

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

这里,主要是引入 @ConfigurationProperties(prefix = "book") 注解,并且配置了属性的前缀,此时会自动将 Spring 容器中对应的数据注入到对象对应的属性中,就不用通过 @Value 注解挨个注入了,减少工作量并且避免出错。

总结

application.properties 是 Spring Boot 中配置的一个重要载体,很多组件的属性都可以在这里定制。它的用法和 yaml 比较类似,关于 yaml 配置,大家可以参考 Spring Boot 中的 yaml 配置简介

本文案例我已上传到 GitHub:https://github.com/lenve/javaboy-code-samples

好了,有问题欢迎留言讨论。

关注公众号牧码小子,专注于 Spring Boot+微服务,定期视频教程分享,关注后回复 Java ,领取松哥为你精心准备的 Java 干货!

Guess you like

Origin www.cnblogs.com/lenve/p/10947517.html