We usually put things in config files that change a lot.

For example, the port number server.port=8080 written in the application. Properties configuration file.

So, my database connection information is in the configuration file, and I have to parse the configuration file to use it in the bean.

The whole scenario is to bind all the configuration in the configuration file to Java beans.

To complete this scenario, writing in Java native code is a bit cumbersome. A wrapper is usually done to read into the properties file and wrap 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
             ... ...
         }
     }
Copy the code

Use the Properties class to load the configuration file A. perties, and then iterate through each k-V in the configuration file so that it can be used where it belongs.

This process is simplified in SpringBoot, which is configuring bindings.

Configure the binding

The configuration binding is done using the @ConfigurationProperties annotation, which is used in conjunction with @Component.

Create a new component, Car, with two attributes, brand and price:

@Component public class Car { private String brand; private Integer price; // get set tostringCopy the code

In the application. Properties configuration file, set some property values, such as:

mycar.brand=QQ
mycar.price=9999
Copy the code

Append to the component using the @ConfigurationProperties annotation:

@Component @ConfigurationProperties(prefix = "mycar") public class Car { private String brand; private Integer price; . .Copy the code

The prefix passed in is the prefix in the configuration file, in this case myCar.

validation

Now to test if the binding is successful, add a controller method to the previous HelloController method:

@RestController public class HelloController { @Autowired Car car; @RequestMapping("/car") public Car car() { return car; } @requestMapping ("/hello") public String hello () {return "hello SpringBoot2 hello"; }}Copy the code

Deployment of the application, the browser to http://localhost:8080/car:

The binding succeeded. Procedure

Another way

In addition to the above methods, you can also use the @ EnableConfigurationProperties + @ ConfigurationProperties way to complete the binding.

Note that @ EnableConfigurationProperties annotations to use on the configuration class, said open attribute configuration features:

//@ConditionalOnBean(name = "pet1") @Import({User.class, Dbhelper.class}) @configuration (proxyBeanMethods = true) @importResource ("classpath:beans.xml") // The classpath of the Configuration file @ EnableConfigurationProperties (Car. Class) / / open the function of the attribute configuration 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"); }}Copy the code

@ EnableConfigurationProperties (Car. Class) to want to open the configuration class, it can automatic Car registered into the container, that is before the @ Component on the Car did not need.

//@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
    private String brand;
    private Integer price;

Copy the code

Redeploy access to the address, still ok.

In the second case, for example, Car is a class in a third-party package, but the source code does not have the @Component annotation, so it can be bound this way.

Finally, remember that when you use the @ConfigurationProperties(prefix = “mycar”) configuration binding, you are binding to the contents of the SpringBoot core configuration file application.properties file.