Home >Java >javaTutorial >How SpringBoot2 underlying annotation @ConfigurationProperties configures binding
We usually put some frequently changed things into the configuration file.
For example, the port number server.port=8080
previously written in the configuration file application.properties
, and other common ones include database connection information, etc.
Then, my database connection information is placed in the configuration file. If I want to use it, I must parse the configuration file and use the parsed content in the bean.
The whole scenario is actually binding all the configurations in the configuration file to the java bean.
To complete this scenario, it is still a bit troublesome to write based on java native code. Usually, an encapsulation is made to read the contents of the properties file and encapsulate it into a JavaBean:
public class getProperties { public static void main(String[] args) throws FileNotFoundException, IOException { Properties pps = new Properties(); pps.load(new FileInputStream("a.properties")); Enumeration enum1 = pps.propertyNames();//得到配置文件的名字 while(enum1.hasMoreElements()) { String strKey = (String) enum1.nextElement(); String strValue = pps.getProperty(strKey); System.out.println(strKey + "=" + strValue); //封装到JavaBean ... ... } }
Here, the Properties
class is used to load the configuration filea. properties
, and then traverse each k-v
in the configuration file. After obtaining it, you can use it in the corresponding place.
This process is simplified in springboot, which is configuration binding.
Configuration binding is completed by using the annotation @ConfigurationProperties
. Note that it needs to be used in conjunction with @Component
.
Create a new component Car
, with two properties: brand and price:
@Componentpublic class Car { private String brand; private Integer price;// get set tostring 就不贴了
In the configuration file application.properties
, set some properties Value, for example:
mycar.brand=QQmycar.price=9999
Use @ConfigurationProperties
annotation and add it to the component:
@Component@ConfigurationProperties(prefix = "mycar")public class Car { private String brand; private Integer price;... ...
The prefix passed in is the prefix in the configuration file, here is mycar.
Now let’s test whether the binding is successful. Continue to add a controller method in the previous HelloController
:
@RestControllerpublic class HelloController { @Autowired Car car; @RequestMapping("/car") public Car car() { return car; } @RequestMapping("/hello") public String Hello() { return "Hello SpringBoot2 你好"; }}
Deploy the application, The browser accesses http://localhost:8080/car
:
and the binding is successful.
In addition to the above method, you can also use @EnableConfigurationProperties
@ConfigurationProperties
to complete the binding.
Note that the @EnableConfigurationProperties
annotation should be used on the configuration class to indicate the function of enabling property configuration:
//@ConditionalOnBean(name = "pet1")@Import({User.class, DBHelper.class})@Configuration(proxyBeanMethods = true)@ImportResource("classpath:beans.xml") //配置文件的类路径@EnableConfigurationProperties(Car.class) //开启属性配置的功能public class MyConfig { @Bean("user1") public User user01(){ User pingguo = new User("pingguo",20); pingguo.setPet(tomcatPet()); return pingguo; } @Bean("pet22") public Pet tomcatPet(){ return new Pet("tomcat"); }}
@EnableConfigurationProperties(Car.class)
Pass in the class to enable configuration, which can automatically register the Car into the container, which means that the @Component
on the previous Car is no longer needed.
//@Component@ConfigurationProperties(prefix = "mycar")public class Car { private String brand; private Integer price;
Re-deploy and access the next address, it still works.
Regarding the second usage scenario, for example, the Car here is a class in a third-party package, but the source code does not have the @Component
annotation. , then you can bind in this way.
Finally, remember that when using @ConfigurationProperties(prefix = "mycar")
this configuration is bound to the springboot core configuration file application.properties
file The binding relationship established by the content.
The above is the detailed content of How SpringBoot2 underlying annotation @ConfigurationProperties configures binding. For more information, please follow other related articles on the PHP Chinese website!