Use @Value annotation value in the configuration file is injected into the static variable

When acquiring annotation @Value value profile is injected into the non-static variable, simply @Value ( "$ {}") can be placed above the response variable. When the non-static variables become a static variable, handled differently.

Profiles

ev: 
  uri: https: // 123456.com 
  AppID: 123456

Injected into the non-static variables

 @Value("${e.uri}")
 private String uri;
 @Value("${e.appId}")
 private String appId;

Injected into the static variables

@Component    // This comment can not be less, or need to move close to instantiate. Similar annotation can @Component 
public  class EContractUtil { 

    // declare a static variable 
    Private  static   String URI;   

    Private  static String for appId; 

    // SET injection mode value 
    @Value ( "e.uri $ {}")     // At this time a certain annotations placed here for the variables will be placed above the null 
    public  void the setUri (URI String) {
         the this .uri = URI; 
    } 

    @Value ( "e.appId $ {}" )
     public  void setAppId (String for appId) {
         the this .appId = for appId; 
    } 

    public static  void accessToken () {
         // only static variables used in a static method, not a non-static variable, and the values of static variables obtained from the configuration file, it is necessary to use the methods described above. 
   } 
}     

You do not directly use non-static variables in a static method as to why, so do not name obtained from the configuration file property values ​​trouble. For the following reasons:

Static can only access static;

Both can access non-static non-static, can access static.

Guess you like

Origin www.cnblogs.com/H-Dream/p/12001286.html