SpringBoot eliminates most manual configuration, but for some specific situations, we still need to perform manual configuration. SpringBoot provides us with the application.properties configuration file, allowing us to customize the configuration to the default Configurations are modified to suit specific production situations, including some third-party configurations. Almost all configurations can be written to the application.peroperties file, which will be automatically loaded by SpringBoot, saving us the trouble of manual loading. But in fact, many times we customize configuration files, which require us to load them manually. SpringBoot will not automatically recognize these files. Let’s take a closer look at these aspects.
1. Configuration file format
SpringBoot can recognize two formats of configuration files, namely yml files and properties files. We can replace the application.properties file with application. yml, both of these files can be automatically recognized and loaded by SpringBoot, but if it is a customized configuration file, it is best to use the properties format file, because SpringBoot does not yet provide the function of manually loading yml format files ( This refers to the annotation method).
To be automatically loaded by SpringBoot, the application.properties configuration file needs to be placed in the specified location: src/main/resource directory. Generally, customized configuration files are also located in this directory.
2. Loading of configuration files
Loading means reading the file into the Spring container. To be more precise, it means loading each configuration item into the Spring context container for ready use. Take.
The application.properties configuration file is automatically loaded when the SpringBoot project is started. Its internal related settings will automatically override the SpringBoot default corresponding settings, so all configuration items will be saved in the Spring container. .
1-Public configuration file: application.properties
1 donghao.name=唯一浩哥2 donghao.sex=男3 donghao.age=80
The customized xxx.properties configuration file will not be automatically loaded by SpringBoot and needs to be loaded manually. , manual loading here generally refers to loading by annotation, which involves one of our focuses today: the annotation for loading custom property files: @PropertySource("classpath:xxx.properties"), this annotation is specially used to Load the properties file at the specified location. Spring does not yet provide annotations for loading the yml file at the specified location, so that is why the previous statement is made.
2-Customized configuration file: donghao.properties
1 donghao1.name=动画2 donghao1.sex=女3 donghao1.age=22
其实无论对于哪里的properties文件,当我们需要使用其中配置内容的时候,就在当前类的顶部加注该注解,将该配置文件加载到内存,这些配置文件一次加载即可多次使用。
3、配置项的使用
配置项的使用其实很简单,只要是加载到Spring容器中的配置项都可以直接使用@Value("${key}")的方式来引用,一般将其配置在字段顶部,表示将配置项的值赋值给该字段。
当然更多的情况是将这些配置项与一个JavaBean绑定起来使用,这样绑定一次,我们就可以随时使用。这里涉及到两种情况,一种是将application.properties中的配置与JavaBean绑定,一种是将自定义配置文件中的配置与Javabean绑定。
第一种:applicaiton.properties属性绑定JavaBean
这种情况相对简单(因为application.properties文件会被自动加载,也就是说配置项会被自动加载到内存,到Spring容器之中,省去了手动加载的配置),然后我们在要与属性绑定的JavaBean的类定义顶部加@Component注解和@ConfigurationProperties(prefix="key")注解,前者的目的是为了这个JavaBean可以被SpringBoot项目启动时候被扫描到并加载到Spring容器之中,重点是后者,这个注解一般不是单独使用的,他一般与后面要说的@EnableConfigurationProperties(JavaBean.class)配合使用,但是二者并非使用在同一位置,@ConfigurationProperties(prefix="key")注解加注在JavaBean类定义之上,按字面可以理解为属性配置注解,更直接点的说法就是属性绑定注解,官方解释是:如果想要绑定或者验证一些来源自.properties文件中的额外属性时,你可以在一个标注的@Configuration的类的注有@Bean注解的方法或者一个类之上加注这个注解。我们完全可以将其理解为绑定专用注解。它的作用就是将指定的前缀的配置项的值与JavaBean的字段绑定,这里要注意,为了绑定的成功,一般将字段的名称与配置项键的最后一个键名相同,这样整个键在去掉前缀的情况下就和字段名称一致,以此来进行绑定。
第二种:自定义配置的属性绑定JavaBean
这种情况与之前的基本相同,只是不能自动加载,需要手动加载,在JavaBean之上加上之前介绍的@PropertySource注解进行配置文件加载。还有一点就是将@Component改为@Configuration,为什么这么做呢?
@Configuration注解的底层就是@Component,但是二者意义不同,@Configuration注解侧重配置之意,@Component侧重组件之意,当然配置也是项目组件之一,在这里我们要将配置文件属性与JavaBean绑定,当然更侧重配置之意。
将配置与JavaBean绑定之后,我们就可以通过JavaBean来获取配置的内容,而且JavaBean已经被@Component注解或者@Configuration注解加载到Spring容器,我们可以使用自动注入的方式在其他类中随便使用。
这里要注意一点:当我们在某个类中要使用这个JavaBean时,需要在这个类中指定这个JavaBean的类型,这个指定也要使用注解来制定,正是之前介绍的@EnableConfigurationProperties注解,这个注解与@ConfigurationProperties注解配套使用。官方给出的解释:这个注解是对@ConfigurationProperties的有效支持。标注有@ConfigurationProperties注解的Beans可以被使用标准的方式注册(使用@Bean注解),或者,为了方便起见,直接用使用@EnableConfigurationProperties指定注册。意思是这个注解提供了一种方便直接的注册Bean的方式。
3-绑定JavaBean:Donghao.java
1 package com.donghao.model; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 import org.springframework.context.annotation.Configuration; 5 import org.springframework.context.annotation.PropertySource; 6 7 @Configuration 8 @PropertySource("classpath:donghao.properties") 9 @ConfigurationProperties(prefix="donghao1")10 public class Donghao {11 private String name;12 private String sex;13 private String age;14 public String getName() {15 return name;16 }17 public void setName(String name) {18 this.name = name;19 }20 public String getSex() {21 return sex;22 }23 public void setSex(String sex) {24 this.sex = sex;25 }26 public String getAge() {27 return age;28 }29 public void setAge(String age) {30 this.age = age;31 }32 }
4-定义控制器:DonghaoController
1 package com.donghao.controller; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.beans.factory.annotation.Value; 5 import org.springframework.boot.context.properties.EnableConfigurationProperties; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RestController; 8 9 import com.donghao.model.Donghao;10 11 @RestController12 @RequestMapping("/donghao")13 @EnableConfigurationProperties(Donghao.class)14 public class DonghaoController {15 16 @Autowired17 Donghao donghao;18 19 @Value("${donghao.name}")20 private String name;21 22 @Value("${donghao.sex}")23 private String sex;24 25 @Value("${donghao.age}")26 private String age;27 28 29 @RequestMapping("/hello")30 public String hello(){31 return "我的名字叫"+name+",我是"+sex+"生,今年"+age+"岁了!";32 }33 34 @RequestMapping("/ss")35 public String ss(){36 return donghao.getName()+donghao.getSex()+donghao.getAge();37 }38 }
5-定义启动入口类:DonghaoApplication.java
1 package com.donghao; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class DonghaoApplication { 8 9 public static void main(String[] args) {10 11 SpringApplication.run(DonghaoApplication.class, args);12 13 }14 15 }
After starting the program, the browser accesses: http://localhost:8080/donghao/hello, and the result is:
The browser accesses: http://localhost :8080/donghao/ss
I want to emphasize here that loading and use are not related. Although the purpose of loading is to use, there is no relationship between loading and use. It is not strongly related. We can load it but it is not practical, so we should analyze the loading process and the use process separately. They correspond to different annotations. These annotations are not strongly related to each other. They each have their own differences. Each has its own purpose. If you just load a custom configuration file, just one @PropertySource annotation is enough. You don’t need to worry about the usage annotations. When you need to use it, we can choose a variety of usage methods. If you use it directly We use the @Value annotation for direct assignment. This annotation can directly assign the value of the property configuration loaded into the Spring container to the specified field. Of course, you can also use binding JavaBean.
Another thing to note is that never configure different values of the same configuration items in the public configuration file application.properties and the custom configuration file xxx.properties, because the public configuration file has the highest priority. Will overwrite the content in the custom configuration file. You can understand that a configuration in the public configuration file is loaded into the Spring container at startup, and then a configuration item with the same name is loaded in another custom configuration file. , the two have different values, but the system will check the priority of the two, whoever is higher will stay, whoever is lower will go, and finally the value in the custom configuration file is invalid.
The above is the detailed content of Detailed introduction to properties configuration. For more information, please follow other related articles on the PHP Chinese website!