IDEA development spring boot application application.yml or application.properties custom properties Tips

Use spring boot in the development process, often define their own attributes some applications, direct written application configuration file using @Value annotations for use, such use is no problem. But I think it is a more elegant way to define your own attribute class unified management, so that idea in both automatic prompts, but also on the configuration of classified management, it is orderly, the following are specific configuration steps.

 

The first step: add dependencies (into maven and gradle two ways)

1.1 If you are using maven

Increased reliance

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

 

1.2 If you are using gradle

And configured dependent increase annotationProcessor

compileOnly 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor ‘org.springframework.boot:spring-boot-configuration-processor’

 

Step 2: Create Configuration Properties class

@Data
@ConfigurationProperties(prefix = “myapp.prefix")
public class MyAppProperties {

    private String prop1;
    private int prop2;

}

 

Third step: an increase in the configuration class annotated

@Configuration
@EnableConfigurationProperties(MyAppProperties.class)
public class MyConfig {

}

 

Step Four: attribute class used

@Component
 public  class the MyComponent { 

    Private  Final MyAppProperties Properties; 

    public the MyComponent (MyAppProperties Properties) {
         the this .properties = Properties; 
    } 
    // now use 

}

By the way, do not forget to configure your application.yml

myapp:
  prefix:
    prop1: 1111
    prop2: 2222

Or application.properties

myapp.prefix.prop1=1111
myapp.prefix.prop2=2222

Also note: If you want to automatically prompt idea to take effect, you need to re-run your application, sometimes not so fast to take effect.

Guess you like

Origin www.cnblogs.com/imxiangli/p/12151498.html