An overview of the
- Option 1: @ConfigurationProperties + @Component annotations to the bean definition class
- Option 2: @ConfigurationProperties + @Bean annotations on the Bean definition method of the configuration class
- Pattern 3: @ ConfigurationProperties annotations to the ordinary class and then by @ EnableConfigurationProperties defined as examples of bean in this paper, based on a Spring boot web application: The default spring-boot Web application is used
Example using the configuration file Increase in the first example project configuration file SRC/main/resources/application. The properties, its content is as follows:
section1.name=Tom
section2.name=Jerry
section3.name=Dog
Copy the code
Option 1: @ConfigurationProperties + @Component annotations to the bean definition class
Class is defined as a bean component
// Define the class as a bean annotation, For example, @component, @service,@Controller, @repository // or @configuration@component // use the section1 prefix in the Configuration file // When using the bean generated by this definition, ConfigurationProperties(prefix = "section1") Public Class Bean1 {// There must be get/set method public String getName() { return name; } public void setName(String name) { this.name = name; } private String name; }Copy the code
Option 2: @ConfigurationProperties + @Bean annotations on the Bean definition method of the configuration class
The bean comes from an ordinary class
Public class Bean2 {public String getName() {return name; } public void setName(String name) { this.name = name; } private String name; }Copy the code
Define the above POJO class as a Bean in a method in an @Configuration Configuration class of the project using the @Bean annotation, and initialize the properties of the Bean with the corresponding properties in the Configuration file.
The @Configuration class can be either directly annotated with the @Configuration annotation or implicitly annotated with the @Configuration annotation, as in the following example, @SpringBootApplication implies @Configuration.
// Declare it a Spring Boot application, The @configuration @SpringBootApplication public Class Application {// the @bean annotation defines a Bean on this method. This method-based Bean definition does not have to appear on // In the @SpringBootApplication annotated class, @bean // initializes the same name of the Bean instance generated by the Bean definition using the value of the property prefixed with section2 in the Configuration file. The property name would be Jerry @ConfigurationProperties(prefix = "section2") public Bean2 Bean2 () {// Note that Bean2 is a POJO class as shown above, Return new Bean2(); } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); }}Copy the code
Pattern 3: @ ConfigurationProperties annotations to the ordinary class and by @ EnableConfigurationProperties defined as bean
Attributes that annotate a normal class will come from an external properties file
// This annotation states that if the class is defined as a bean, the attribute value for the corresponding bean instance will come from a property of the same name prefixed with // section3 in the configuration file. @ConfigurationProperties(prefix = "section3") public Class Bean3 {public String getName() { return name; } public void setName(String name) { this.name = name; } private String name; }Copy the code
Using the @ EnableConfigurationProperties defines the class as a bean
@springBootApplication // This annotation registers class Bean3 as a bean definition to the bean container, The annotation // @ConfigurationProperties(prefix = "section3") on class Bean3 causes the property values of the target bean // instance to be populated with the same property values prefixed with section3 in the configuration file, namely the target // The value of the bean attribute name is Dog @ EnableConfigurationProperties ({Bean3. Class}) public class Application {public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); }}Copy the code
\