In the spring mvc architecture, if you want to use the configuration values defined in properties directly in the program, you usually use the following methods to obtain:
@Value("${tag}")
private String tagValue;
But when taking the value, sometimes the tagvalue is NULL, the possible reasons are:
- The tagValue is modified with static or final, as follows:
private static String tagValue; //错误
private final String tagValue; //错误
- The class does not add @Component (or @service, etc.) Need to confirm whether the bean is loaded
@Component //遗漏
class TestValue{
@Value("${tag}")
private String tagValue;
}
- The class was created with a new instance without @Autowired
@Component
class TestValue{
@Value("${tag}")
private String tagValue;
}
class Test{
...
TestValue testValue = new TestValue()
}
There is definitely no value in this testValue, you must use @Autowired:
Also pay attention to the imported package path: import org.springframework.beans.factory.annotation.Value;
I have encountered the copied code before, the package path introduces an error, no red cross is reported, but the value cannot be assigned.
class Test{
@AutoWired
TestValue testValue
}