Spring @value和@PropertySource注解使用方法解析
更新時間:2019年11月30日 15:00:07 投稿:yaominghui
這篇文章主要介紹了Spring @value和@PropertySource注解使用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
這篇文章主要介紹了Spring @value和@PropertySource注解使用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
- @Value注解:可以使用注入基本字符串 EL表達式,從配置文件讀取數(shù)據(jù)
- @PropertySource用于引入單個配置文件
- @PropertySources用于引入多個配置文件
- @PropertySource或者@PropertySources引入的數(shù)據(jù)都是存在環(huán)境變量ConfigurableEnvironment中的
- 在resouces文件夾下新建一個cat.properties文件,寫入內(nèi)容parent=tiger
public class Cat { @Value("貓") //直接注入字符串 private String name; @Value("#{12+2}") //支持EL表達式 private int age; @Value("${parent}") //支持配置文件讀取 private String parent; public Cat() { System.out.println("貓被初始化"); } @Override public String toString() { return "Cat{" + "name='" + name + '\'' + ", age=" + age + ", parent='" + parent + '\'' + '}'; } }
@Import({Cat.class}) @PropertySources({@PropertySource(value ="cat.properties")}) public class Appconfig { }
public class Demo { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Appconfig.class); Cat bean = context.getBean(Cat.class); System.out.println(bean); //Cat{name='貓', age=14, parent='tiger'} ConfigurableEnvironment environment = context.getEnvironment(); System.out.println(environment.getProperty("parent")); //tiger } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Myeclipse鏈接Oracle等數(shù)據(jù)庫時lo exception: The Network Adapter coul
今天小編就為大家分享一篇關(guān)于Myeclipse鏈接Oracle等數(shù)據(jù)庫時lo exception: The Network Adapter could not establish the connection,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03java中List對象列表實現(xiàn)去重或取出及排序的方法
這篇文章主要介紹了關(guān)于java中List對象列表實現(xiàn)去重或取出以及排序的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08詳細聊聊Spring MVC重定向與轉(zhuǎn)發(fā)
大家應(yīng)該都知道請求重定向和請求轉(zhuǎn)發(fā)都是web開發(fā)中資源跳轉(zhuǎn)的方式,這篇文章主要給大家介紹了關(guān)于Spring MVC重定向與轉(zhuǎn)發(fā)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2021-09-09SpringBoot如何優(yōu)雅的整合Swagger Api自動生成文檔
在多人協(xié)作的開發(fā)過程中,API文檔不僅可以減少等待,也能保證開發(fā)的持續(xù)進行,這篇文章主要給大家介紹了關(guān)于SpringBoot如何優(yōu)雅的整合Swagger Api自動生成文檔的相關(guān)資料,需要的朋友可以參考下2021-07-07