spring @Value 获取配置文件为 null 常见的几种方式

第一种方式:

xx.properties 属性名称错误,未与@Value("${xxx}") 进行对应

第二种方式:

该类未注入到spring bean容器中


@Component
@Controller
@Service
# 上面未注入,使用 @Autowired 会报错
@Autowired
 

第三种方式:

采用 new 等方式获取该对象

 @Component   
    class TestValue{
         @Value("${tag}")
         private String tagValue;
    }

    class Test{
        ...
        TestValue testValue = new TestValue()
    }
\

第四种方式

将该属性 用 static final 等字眼进行 修饰

@Value("${value}")
private
static String value; //错误
@Value("${value}")
private final String value; //错误
扫描二维码关注公众号,回复: 6953231 查看本文章

第五种方式 

该属性名 大写

@Value("${value}")
private static String VALUE;  //错误

参考文档:https://blog.csdn.net/zzmlake/article/details/54946346

第五种方式 是我真正遇到的,若还有其他方式会导致@Value("${xxx}") 为null ,

请在下方留言,

猜你喜欢

转载自www.cnblogs.com/zhangzhonghui/p/11301136.html