This article is participating in the Java Theme Month – Java Debug Notes EventActive link
preface
In real development, we often need to assign values to static properties from either yML or properties files. Instead of using the @Value annotation alone, here are a few ways to inject properties.
We usually use it in the following way:
@Value("${channl.ali.appId}")
String appId;
Copy the code
Set method injection
Annotate the @value annotation to the set method
@Component
public class StaticValue {
public static String APP_ID;
public static String APP_USER;
@Value("${channl.ali.appId}")
public void setAppId(String appId) {
APP_ID = appId;
}
@Value("${channl.ali.appUser}")
public void setAppUser(String appUser) { APP_USER = appUser; }}Copy the code
Note: This class needs to be managed by Spring, and the setXx method name can be arbitrary
use
Use classes directly where they are needed. The attribute name
@Test
void contextLoads(a) {
System.out.println(StaticValue.APP_ID);
System.out.println(StaticValue.APP_USER);
}
Copy the code
@PostConstruct
The PostConstruct annotation is an annotation provided by Java. Methods annotated with the @postConstruct annotation will be called automatically after the constructor and dependency injection have completed. As a small example, the specific source code will be analyzed in a later source code article. If you are interested, you can take a look at other source analysis: Spring source code reading directory
use
@Value("${channl.ali.appId}")
private String appId;
@Value("${channl.ali.appUser}")
private String appUser;
//@Autowired
private Environment environment;
public StaticValue(a) {
System.out.println("Constructor execution =" + environment);
}
@Autowired
private void setq(Environment environment){
System.out.println("Dependency injection method execution =" + environment);
this.environment = environment;
}
@PostConstruct
public void init(a){
System.out.println("PostConstruct annotation init method execution.");
APP_ID = environment.getProperty("channl.ali.appId");
APP_USER = environment.getProperty("channl.ali.appUser");
//APP_ID = appId;
//APP_USER = appUser;} at project startup you should see the control print the following statement: constructor execution =nullDependency injection method performs = StandardServletEnvironment {activeProfiles = [], defaultProfiles = [default], propertySources=[ConfigurationPropertySourcesPropertySource {name='configurationProperties'}, StubPropertySource {name='servletConfigInitParams'}, ServletContextPropertySource {name='servletContextInitParams'}, PropertiesPropertySource {name='systemProperties'}, OriginAwareSystemEnvironmentPropertySource {name='systemEnvironment'}, RandomValuePropertySource {name='random'}, OriginTrackedMapPropertySource {name='Config resource 'classpath:/application.yml' via location 'optional:classpath:/' '}]} PostConstruct annotation init method is executedCopy the code
Note: Both Environment and @value are acceptable. So constructor > @AutoWired (dependency injection) > @postConstruct (annotated method)
Implement the InitializingBean interface
Implement the InitializingBean interface and rewrite its afterPropertiesSet method. Get the value of the configuration file using @Value, and then assign the value to the static property in the afterPropertiesSet method. The InitializingBean interface will also be analyzed in a later source code article. If you are interested, you can take a look at other source analysis: Spring source code reading directory
use
@Value("${channl.ali.appId}")
private String appId;
@Value("${channl.ali.appUser}")
private String appUser;
//@Autowired
private Environment environment;
@Override
public void afterPropertiesSet(a) throws Exception {
System.out.println("AfterPropertiesSet initialization method execution");
// //APP_ID = environment.getProperty("channl.ali.appId");
// // APP_USER = environment.getProperty("channl.ali.appUser");
APP_ID = appId;
APP_USER = appUser;
}
@PostConstruct
public void init(a){
System.out.println("PostConstruct annotation init method execution.");
//APP_ID = environment.getProperty("channl.ali.appId");
//APP_USER = environment.getProperty("channl.ali.appUser");
//APP_ID = appId;
//APP_USER = appUser;} at project startup you should see the control print the following statement: constructor execution =nullDependency injection method performs = StandardServletEnvironment {activeProfiles = [], defaultProfiles = [default], propertySources=[ConfigurationPropertySourcesPropertySource {name='configurationProperties'}, StubPropertySource {name='servletConfigInitParams'}, ServletContextPropertySource {name='servletContextInitParams'}, PropertiesPropertySource {name='systemProperties'}, OriginAwareSystemEnvironmentPropertySource {name='systemEnvironment'}, RandomValuePropertySource {name='random'}, OriginTrackedMapPropertySource {name='Config resource 'classpath:/application.yml' via location 'optional:classpath:/' '}]} PostConstruct annotation init method executes afterPropertiesSet initialization method executionCopy the code
Note: Both Environment and @value are acceptable.
- If you have any questions or errors in this article, please feel free to comment. If you find this article helpful, please like it and follow it.
Code has been hosted in Gitee: assigning values to static properties