Class property value after the agency is gone?
Foreword
When using SpringBoot development, if we are to use custom values, our common practice is to Yml
configure the properties profile, and then configure the appropriate configuration file to obtain the property value in the class. For example we Yml
have the following attribute values
test:
value: secondAgent
复制代码
Then we can make the following configuration can be removed from class values
@Component
public class GetValue {
@Value("${test.value}")
public String testValue;
}
复制代码
Such automatic injection directly through the attribute values can be obtained at the time of use
@Autowired
private GetValue getValue;
@Override
public void run(String... args) throws Exception {
System.out.println(getValue.testValue);
}
复制代码
Problem Description
But once developed, after the project start how to obtain values are not acquired. In the retrospective case file was last modified recently found added affairs. Transaction Management added the entire service folder. And this profile folder out of service after the value can get to.
- service
- common
- 配置类所在的位置
复制代码
Configuration class something like
@Data
@Component
public class GetValue {
@Value("${test.value}")
public String testValue;
}
复制代码
Note the non-standard coding here where property values using a modification of the public, but did not use the time to get the value through the get () method
It will generate a proxy class on the basis of such a transaction at the time of use, and the proxy class attribute value is passed, but come. We can see the proxy configuration class as follows
And if we get this time directly through automatically injected into a proxied class, and call the proxy class only calls the method to be able to reach the level of the original class, if the acquisition is to obtain the property value directly visible. So this time, if we called directly getValue.testValue
to obtain value can only be null.
Solution
- Or will such proxy files out of the folder. Does not generate a proxy class
- Using the
get()
method of retrieving properties