@ConfigurationProperties is an annotation provided by Spring Boot to get configuration file data. If you specify the prefix in the configuration file, the corresponding configuration file data will be automatically populated into the Bean.

ConfigurationProperties can be applied to classes or methods.

Class

1. Inapplication.yml(orapplication.properties) to add configuration information.

Email: Sender: topic: ConfigurationProperties Content: Spring Boot @ConfigurationProperties demoCopy the code

2. Define configuration classes.

package com.example.demo; import javax.annotation.PostConstruct; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "email") public class EmailConfig { private String sender; private String topic; private String content; @PostConstruct public void postConstruct() { System.out.println(this.toString()); } @override public String toString() {return "{" + "sender='" +" + "+ ", topic='" + getTopic() + "'" + ", content='" + getContent() + "'" + "}"; }}Copy the code

3. Start the Spring Boot application. The following information is displayed in the startup log:

{sender=' ConfigurationProperties', topic=' Spring Boot @ConfigurationProperties demo'}Copy the code

Two, the method of action

1. Define the Bean.

package com.example.demo; public class Email { private String sender; private String topic; private String content; @override public String toString() {return "{" + "sender='" +" + "+ ", topic='" + getTopic() + "'" + ", content='" + getContent() + "'" + "}"; }}Copy the code

2. Define methods and use@ConfigurationPropertiesAnnotations are used to inject configuration data@BeanAnnotations are used together.

package com.example.demo; import javax.annotation.PostConstruct; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class EmailConfig { @PostConstruct public void postConstruct() { System.out.println(this.toString()); } @Bean @ConfigurationProperties(prefix = "email") public Email email() { return new Email(); }}Copy the code

3. Define an autowiringEmailThe configuration class of the type property, which is printed after construction to determine whether the configuration data in step 2 has been successfully injected.

package com.example.demo; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; @Configuration public class Checker { @Autowired private Email email; @PostConstruct public void PostConstruct() { System.out.println(email.toString()); }}Copy the code

4. Start the application. The following information is displayed in the log:

{sender=' ConfigurationProperties', topic=' Spring Boot @ConfigurationProperties demo'}Copy the code