1. 第一步檢測語法是否正確
@Value("${hdfs.name}")
private String hdfs;
2.第二步檢測配置文件中是否有進行配置(application.properties)
hdfs.name=jilin
3.第三步檢測是否增加了@Component注解
注意:在spring中,使用了spring的注解,那么就需要使用spring來進行管理對象,而不能自己進行new,否則就會導致失敗。
@Autowired為自動裝配,將對象自動注入到類中使用.
@Autowired注入有兩個條件,被注入的類的對象交給了spring管理;同時使用的類的對象也要交給spring管理.兩個條件都滿足才能注入.
@Component public class Test { @Value("${hdfs.name}") private String hdfs; private static Properties properties = new Properties(); static { InputStream resourceAsStream = Test.class.getResourceAsStream("/common/testFile.properties"); try { properties.load(resourceAsStream); }catch (IOException ioe){ ioe.printStackTrace(); }finally{ } } public String getHdfs() { return hdfs; } public void setHdfs(String hdfs) { this.hdfs = hdfs; } }
測試一下是否注入成功,僅為測試,所以直接寫在下了啟動類下
@SpringBootApplication @Component @RestController public class DemoApplication { @Autowired private Test test; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @RequestMapping("/get") public void get(){ System.out.println(test.getHdfs()); } }
postman發送請求,看是否調用成功。成功!!!
4.第四步檢測代碼中的書寫方式
不要再無參構造器中,進行new對象的操作。否則就會造成@Value注解失敗。(本人就是踩到這步坑)。
初始化上下文可以使用@PostConstruct注解來進行初始化,他會在spring加載完信息后,進行調用並且只調用一次。
5.@Value無法注入static屬性
注意:使用@Value直接放在static的屬性上是無法注入內容的!!!此方式會一直是null.如下圖
發現@value不能直接注入值給靜態屬性,spring 不允許/不支持把值注入到靜態變量中;
spring支持set方法注入,我們可以利用非靜態setter 方法注入靜態變量,並且使用@Value的類必須交個spring進行管理.就如同@Autowired有時無法注入一樣.
改進
使用setter方法進行屬性的賦值,並且setter方法不能有static
idea自動生成的方法會有static,需要手動刪除.
@Component public class Test { private static String hdfs; private static Properties properties = new Properties(); static { InputStream resourceAsStream = Test.class.getResourceAsStream("/common/testFile.properties"); try { properties.load(resourceAsStream); }catch (IOException ioe){ ioe.printStackTrace(); }finally{ } } public String getHdfs() { return hdfs; } @Value("${hdfs.name}") public void setHdfs(String hdfs) { this.hdfs = hdfs; } }
重新測試,結果無誤!!!!
另外一種讀取配置文件方式:使用@PropertySource注解
如果不是application.properties,而是其他的properties的配置文件,采用PropertySource注解
classpath:后可以跟路徑 如@PropertySource("classpath:common/testFile.properties") 表示 resource目錄下common文件夾下有個testFile.properties文件
@Component @PropertySource("classpath:zookeeper.properties") public class ZookeeperConfig { //zk connect config @Value("${zookeeper.quorum:39.100.43.16:2181}") private String serverList; @Value("${zookeeper.retry.base.sleep:100}") private int baseSleepTimeMs; @Value("${zookeeper.retry.max.sleep:30000}") private int maxSleepMs; @Value("${zookeeper.retry.maxtime:10}") private int maxRetries; @Value("${zookeeper.session.timeout:60000}") private int sessionTimeoutMs; @Value("${zookeeper.connection.timeout:30000}") private int connectionTimeoutMs; @Value("${zookeeper.connection.digest: }") private String digest; @Value("${zookeeper.dolphinscheduler.root:/dolphinscheduler}") private String dsRoot; @Value("${zookeeper.max.wait.time:10000}") private int maxWaitTime; }
轉載:https://www.cnblogs.com/nhdlb/p/11741228.html