Spring中@Value注解獲取不到配置值問題及解決
@Value注解必須要在spring的bean中才能使用,不能自己new一個對象調(diào)用
產(chǎn)生原因:
在SpringBoot中使用@Value只能給普通變量賦值,不能給靜態(tài)變量賦值
解決方法:
給靜態(tài)變量增加一個非靜態(tài)的set方法,注意需要把@Value注解寫到對應的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、碰到過三種情況導致@Value獲取不到配置值
- 變量被關鍵字static修飾
- 類沒有使用@Component及其衍生標簽修飾
- 在Bean初始化時構造方法中引用被@Value修飾的變量
需要獲取的配置如下
kafka:
bootstrap:
servers: 192.168.202.131:9092
servers:
first:
topic: "first_topic"
group: "first_group" 
測試項目是springboot的,如果是普通spring項目的application.properties文件也是類似
2、變量被關鍵字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及其衍生標簽修飾
因為@Value是在AbstractAutowireCapableBeanFactory類的doCreateBean方法中進行初始化,
即交由Spring容器進行處理,
如果沒有@Component及其衍生注解注釋Spring無法識別,導致無法獲取到配置值。
4、在Bean初始化時構造方法中引用被@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初始化時在構造方法中無法獲取到@Value修飾的參數(shù)值
解決方法:
如果需要在bean初始化時獲取參數(shù)值,那么我們可以使用@Config注解在bean初始化時獲取到,
代碼如下:
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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Mybatis中傳遞多個參數(shù)的4種方法總結(jié)
這篇文章主要給大家介紹了關于Mybatis中傳遞多個參數(shù)的4種方法,并且介紹了關于使用Mapper接口時參數(shù)傳遞方式,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。2018-04-04
Java基于servlet監(jiān)聽器實現(xiàn)在線人數(shù)監(jiān)控功能的方法
這篇文章主要介紹了Java基于servlet監(jiān)聽器實現(xiàn)在線人數(shù)監(jiān)控功能的方法,結(jié)合實例形式分析了ServletContextListener監(jiān)聽功能的相關使用步驟與操作技巧,需要的朋友可以參考下2018-01-01
前端與RabbitMQ實時消息推送未讀消息小紅點實現(xiàn)示例
這篇文章主要為大家介紹了前端與RabbitMQ實時消息推送未讀消息小紅點實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07

