本文共 790 字,大约阅读时间需要 2 分钟。
在Spring MVC架构中,如果希望在程序中直接使用properties中定义的配置值,通常可以通过以下方式获取:
@Value("${tag}")private String tagValue; 然而,在实际应用中,tagValue可能会出现NULL的情况,这可能由以下原因引起:
private static String tagValue; // 错误private final String tagValue; // 错误
@Componentclass TestValue { @Value("${tag}") private String tagValue;} @Componentclass TestValue { @Value("${tag}") private String tagValue;} 此外,还需要注意引入正确的包路径:
import org.springframework.beans.factory.annotation.Value;
如果之前遇到过拷贝过来的代码,且包路径引入错误,可能会导致赋值失败。例如:
@AutowiredTestValue testValue = new TestValue();
为了确保赋值正确,必须使用@Autowired注解:
@Autowiredprivate TestValue testValue;
转载地址:http://boqfk.baihongyu.com/