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; //错误

 

第五种方式 

该属性名 大写

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

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

 

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

请在下方留言,

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM