SpringBoot之@Value獲取application.properties配置無效的解決
@Value獲取application.properties配置無效問題
無效的原因主要是要注意@Value使用的注意事項(xiàng):
- 1、不能作用于靜態(tài)變量(static);
- 2、不能作用于常量(final);
- 3、不能在非注冊(cè)的類中使用(需使用@Componet、@Configuration等);
- 4、使用有這個(gè)屬性的類時(shí),只能通過@Autowired的方式,用new的方式是不會(huì)自動(dòng)注入這些配置的。
這些注意事項(xiàng)也是由它的原理決定的:
springboot啟動(dòng)過程中,有兩個(gè)比較重要的過程,如下:
- 1 、掃描,解析容器中的bean注冊(cè)到beanFactory上去,就像是信息登記一樣。
- 2、 實(shí)例化、初始化這些掃描到的bean。
@Value的解析就是在第二個(gè)階段。BeanPostProcessor定義了bean初始化前后用戶可以對(duì)bean進(jìn)行操作的接口方法,它的一個(gè)重要實(shí)現(xiàn)類AutowiredAnnotationBeanPostProcessor正如javadoc所說的那樣,為bean中的@Autowired和@Value注解的注入功能提供支持。
下面說下兩種方式:
resource.test.imageServer=http://image.everest.com
1、第一種
@Configuration public class EverestConfig { ? ? ? @Value("${resource.test.imageServer}") ? ? private String imageServer; ? ? ? public String getImageServer() { ? ? ? ? return imageServer; ? ? } ? }
2、第二種
@Component @ConfigurationProperties(prefix = "resource.test") public class TestUtil { ? ? ? public String imageServer; ? ? ? public String getImageServer() { ? ? ? ? return imageServer; ? ? } ? ? ? public void setImageServer(String imageServer) { ? ? ? ? this.imageServer = imageServer; ? ? } }
然后在需要的地方注入就可
? ? @Autowired ? ? private TestUtil testUtil; ? ? ? @Autowired ? ? private EverestConfig everestConfig; ? ? ? ? @GetMapping("getImageServer") ? ? public String getImageServer() { ? ? ? ? return testUtil.getImageServer(); // ? ? ? ?return everestConfig.getImageServer(); ? ? }?
@Value獲取application.properties中的配置取值為Null
@Value("${spring.datasource.url}") private String url;
獲取值為NUll。
解決方法
不要使用new的方法去創(chuàng)建工具類(DBUtils)對(duì)象,而是使用@Autowired的方式交由springboot來管理,在工具類上加上@Component,定義的屬性變量不要加static。
正確做法
@Autowired private DBUtils jdbc; ?? @Component public class DBUtils{ ? ?? ? ? @Value("${spring.datasource.url}") ? ? private String url; }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
kotlin快速入門之標(biāo)準(zhǔn)函數(shù)與靜態(tài)方法
學(xué)完了Kotlin的基礎(chǔ)知識(shí),是時(shí)候來來學(xué)習(xí) Kotlin的標(biāo)準(zhǔn)函數(shù)和靜態(tài)方法了,下面這篇文章主要給大家介紹了關(guān)于kotlin快速入門之標(biāo)準(zhǔn)函數(shù)與靜態(tài)方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09Java對(duì)象布局(JOL)實(shí)現(xiàn)過程解析
這篇文章主要介紹了Java對(duì)象布局(JOL)實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04java自帶的MessageDigest實(shí)現(xiàn)文本的md5加密算法
這篇文章主要介紹了java自帶的MessageDigest實(shí)現(xiàn)文本的md5加密算法,需要的朋友可以參考下2015-12-12Java中StringBuilder與StringBuffer使用及源碼解讀
我們前面學(xué)習(xí)的String就屬于不可變字符串,因?yàn)槔碚撋弦粋€(gè)String字符串一旦定義好,其內(nèi)容就不可再被改變,但實(shí)際上,還有另一種可變字符串,包括StringBuilder和StringBuffer兩個(gè)類,那可變字符串有什么特點(diǎn),又怎么使用呢,接下來就請(qǐng)大家跟我一起來學(xué)習(xí)吧2023-05-05Spring Boot 編寫Servlet、Filter、Listener、Interceptor的方法
這篇文章給大家介紹了spring-boot中如何定義過濾器、監(jiān)聽器和攔截器,對(duì)Spring Boot 編寫Servlet、Filter、Listener、Interceptor的相關(guān)知識(shí)感興趣的朋友一起看看吧2017-07-07SpringBoot在生產(chǎn)快速禁用Swagger2的方法步驟
這篇文章主要介紹了SpringBoot在生產(chǎn)快速禁用Swagger2的方法步驟,使用注解關(guān)閉Swagger2,避免接口重復(fù)暴露,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-12-12