Spring-Boot:多種配置注入方式

package com.example.el;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Service;

/**

* Created by liz19 on 2017/1/31.

*/

@Service

public class DemoService {

? ?@Value("其他類屬性")

? ?private String another;

? ?public String getAnother(){

? ? ? ?return another;

? ?}

? ?public void setAnother(String another){

? ? ? ?this.another=another;

? ?}

}

package com.example.el;

import org.apache.commons.io.IOUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.PropertySource;

import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

import org.springframework.core.env.Environment;

import org.springframework.core.io.Resource;

import java.io.IOException;

/**

* Created by liz19 on 2017/1/31.

*/

@Configuration

@ComponentScan("com.example.el")

@PropertySource("classpath:test.properties")

public class ElConfig {

? ?//注入普通字符串

? ?@Value("Testing String inject")

? ?private String normal;

? ?//注入系統(tǒng)屬性

? ?@Value("#{systemProperties['os.name']}")

? ?private String osName;

? ?//注入表達(dá)式結(jié)果

? ?@Value("#{ T(java.lang.Math).random()}")

? ?private double randomNumber;

? ?//注入其他bean屬性

? ?@Value("#{demoService.another}")

? ?private String fromAnother;

? ?//注入文件資源

? ?@Value("classpath:test2.txt")

? ?private Resource testFile;

? ?//注入網(wǎng)址資源

? ?@Value("http://www.baidu.com")

? ?private Resource testUrl;

? ?//指定Properties文件注入并注入值

? ?@Value("${book.name}")

? ?private String bookName;

? ?//PropertySource注入需要指定

? ?@Autowired

? ?private Environment environment;

? ?//PropertySource注入需要實現(xiàn)

? ?public static PropertySourcesPlaceholderConfigurer propertyConfigure(){

? ? ? ?return new PropertySourcesPlaceholderConfigurer();

? ?}

? ?public void outputResource(){

? ? ? ?try {

? ? ? ? ? ?System.out.println(normal);

? ? ? ? ? ?System.out.println(osName);

? ? ? ? ? ?System.out.println(randomNumber);

? ? ? ? ? ?System.out.println(fromAnother);

? ? ? ? ? ?System.out.println(IOUtils.toString(testFile.getInputStream()));

? ? ? ? ? ?System.out.println(IOUtils.toString(testUrl.getInputStream()));

? ? ? ? ? ?System.out.println(bookName);

? ? ? ? ? ?System.out.println(environment.getProperty("book.author"));

? ? ? ?} catch (IOException e) {

? ? ? ? ? ?e.printStackTrace();

? ? ? ?}

? ?}

}

package com.example;

import com.example.el.ElConfig;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**

* Created by liz19 on 2017/1/31.

*/

public class ElApp {

? ?public static void main(String[] args){

? ? ? ?AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ElConfig.class);

? ? ? ?ElConfig resourceService = context.getBean(ElConfig.class);

? ? ? ?resourceService.outputResource();

? ? ? ?context.close();

? ?}

}

##test.properties

book.name=spring boot test

book.author=test Author

##test2.txt

this is test.txt content

1.多種配置注入,包括了注入普通字符串背苦,注入系統(tǒng)屬性互捌,注入表達(dá)式結(jié)果,注入其他bean屬性行剂,注入文件資源秕噪,注入網(wǎng)址資源,指定Properties文件注入并注入值厚宰,PropertySource注入需要指定腌巾,PropertySource注入需要實現(xiàn)遂填。

2. 根據(jù)需求,可將應(yīng)用配置配置到txt文件中澈蝙,再已注入的方式注入到Service中吓坚,供后期代碼使用。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末灯荧,一起剝皮案震驚了整個濱河市礁击,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌逗载,老刑警劉巖哆窿,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異厉斟,居然都是意外死亡更耻,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進(jìn)店門捏膨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來秧均,“玉大人,你說我怎么就攤上這事号涯∧亢” “怎么了?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵链快,是天一觀的道長誉己。 經(jīng)常有香客問我,道長域蜗,這世上最難降的妖魔是什么巨双? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮霉祸,結(jié)果婚禮上筑累,老公的妹妹穿的比我還像新娘。我一直安慰自己丝蹭,他們只是感情好慢宗,可當(dāng)我...
    茶點故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著奔穿,像睡著了一般镜沽。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上贱田,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天缅茉,我揣著相機(jī)與錄音,去河邊找鬼男摧。 笑死蔬墩,一個胖子當(dāng)著我的面吹牛译打,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播筹我,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼扶平,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蔬蕊?” 一聲冷哼從身側(cè)響起结澄,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎岸夯,沒想到半個月后麻献,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡猜扮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年勉吻,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片旅赢。...
    茶點故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡齿桃,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出煮盼,到底是詐尸還是另有隱情短纵,我是刑警寧澤,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布僵控,位于F島的核電站香到,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏报破。R本人自食惡果不足惜悠就,卻給世界環(huán)境...
    茶點故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望充易。 院中可真熱鬧梗脾,春花似錦、人聲如沸蔽氨。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鹉究。三九已至,卻和暖如春踪宠,著一層夾襖步出監(jiān)牢的瞬間自赔,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工柳琢, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留绍妨,地道東北人润脸。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像他去,于是被迫代替她去往敵國和親毙驯。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,092評論 2 355

推薦閱讀更多精彩內(nèi)容