Spring中@Value注解獲取不到配置值問題及解決
@Value注解必須要在spring的bean中才能使用,不能自己new一個(gè)對(duì)象調(diào)用
產(chǎn)生原因:
在SpringBoot中使用@Value只能給普通變量賦值,不能給靜態(tài)變量賦值
解決方法:
給靜態(tài)變量增加一個(gè)非靜態(tài)的set方法,注意需要把@Value注解寫到對(duì)應(yīng)的set方法上,而不是定義的靜態(tài)變量上,
如下所示:
@Value("${authox.sql.url}")
public void setUrl(String url) {
JdbcUtils.url = url;
}
@Value("${authox.sql.username}")
public void setUser(String user) {
JdbcUtils.user = user;
}
@Value("${authox.sql.password}")
public void setPassword(String password) {
JdbcUtils.password = password;
}
@Value("${authox.sql.driver-class-name}")
public void setDriver(String driver) {
JdbcUtils.driver = driver;
}1、碰到過三種情況導(dǎo)致@Value獲取不到配置值
- 變量被關(guān)鍵字static修飾
- 類沒有使用@Component及其衍生標(biāo)簽修飾
- 在Bean初始化時(shí)構(gòu)造方法中引用被@Value修飾的變量
需要獲取的配置如下
kafka:
bootstrap:
servers: 192.168.202.131:9092
servers:
first:
topic: "first_topic"
group: "first_group" 
測試項(xiàng)目是springboot的,如果是普通spring項(xiàng)目的application.properties文件也是類似
2、變量被關(guān)鍵字static修飾
package com.coline.codeskills.kafka;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author: Coline
* @ClassName: TestValue
* @Date: 2020/3/1 23:58
* @Description: test @Value Tag
*/
@Component
public class TestValue implements InitializingBean {
@Value("${kafka.bootstrap.servers}")
private String kafkaServers;
@Value("${kafka.servers.first.topic}")
private static String topic;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println(kafkaServers);
System.out.println(topic);
}
}
可以看到被static修飾的參數(shù)使用@Value無法獲取到配置值
3、類沒有使用@Component及其衍生標(biāo)簽修飾
因?yàn)锧Value是在AbstractAutowireCapableBeanFactory類的doCreateBean方法中進(jìn)行初始化,
即交由Spring容器進(jìn)行處理,
如果沒有@Component及其衍生注解注釋Spring無法識(shí)別,導(dǎo)致無法獲取到配置值。
4、在Bean初始化時(shí)構(gòu)造方法中引用被@Value修飾的變量
package com.coline.codeskills.kafka;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author: Coline
* @ClassName: TestValue
* @Date: 2020/3/1 23:58
* @Description: test @Value Tag
*/
@Component
public class TestValue {
@Value("${kafka.bootstrap.servers}")
private String kafkaServers;
@Value("${kafka.servers.first.topic}")
private static String topic;
public TestValue(){
System.out.println(kafkaServers);
}
}
如圖,bean初始化時(shí)在構(gòu)造方法中無法獲取到@Value修飾的參數(shù)值
解決方法:
如果需要在bean初始化時(shí)獲取參數(shù)值,那么我們可以使用@Config注解在bean初始化時(shí)獲取到,
代碼如下:
package com.coline.codeskills.common.config;
import com.coline.codeskills.kafka.TestValue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
/**
* @author: Coline
* @ClassName: TestValueConfig
* @Date: 2020/3/2 0:44
* @Description: TODO
*/
@Configuration
public class TestValueConfig {
@Value("${kafka.bootstrap.servers}")
private String kafkaServers;
@Primary
@Bean
public TestValue testValue(){
TestValue testValue = new TestValue();
return testValue;
}
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring aop實(shí)現(xiàn)用戶權(quán)限管理的示例
本篇文章主要介紹了spring aop實(shí)現(xiàn)用戶權(quán)限管理的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
Mybatis中傳遞多個(gè)參數(shù)的4種方法總結(jié)
這篇文章主要給大家介紹了關(guān)于Mybatis中傳遞多個(gè)參數(shù)的4種方法,并且介紹了關(guān)于使用Mapper接口時(shí)參數(shù)傳遞方式,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
引入SpringCloud-gateway報(bào)錯(cuò)的解決方案
這篇文章主要介紹了引入SpringCloud-gateway報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java基于servlet監(jiān)聽器實(shí)現(xiàn)在線人數(shù)監(jiān)控功能的方法
這篇文章主要介紹了Java基于servlet監(jiān)聽器實(shí)現(xiàn)在線人數(shù)監(jiān)控功能的方法,結(jié)合實(shí)例形式分析了ServletContextListener監(jiān)聽功能的相關(guān)使用步驟與操作技巧,需要的朋友可以參考下2018-01-01
前端與RabbitMQ實(shí)時(shí)消息推送未讀消息小紅點(diǎn)實(shí)現(xiàn)示例
這篇文章主要為大家介紹了前端與RabbitMQ實(shí)時(shí)消息推送未讀消息小紅點(diǎn)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
mybatis update set 多個(gè)字段實(shí)例
這篇文章主要介紹了mybatis update set 多個(gè)字段實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01

