Spingboot|第六篇學(xué)習(xí)@PropertySource&@ImportResource&@Bean

Springboot 是簡化Spring應(yīng)用開發(fā)的創(chuàng)建、運(yùn)行、調(diào)試、部署等一系列問題的框架是J2EE開發(fā)的一站式解決方案,自動裝配的特性可以讓我們更好的關(guān)注業(yè)務(wù)本身而不是外部的XML配置,我們只需遵循規(guī)范,引入相關(guān)的依賴就可以輕松的搭建出一個WEB工程
[如果你覺得對你有幫助,歡迎轉(zhuǎn)發(fā)分享給更多的人學(xué)習(xí)]

上篇學(xué)習(xí)Spring Boot的配置文件分析@Value獲取值@ConfigurationProperties獲取值比較,接下來是學(xué)習(xí)@PropertySource&@ImportResource&@Bean


@PropertySource:加載指定的配置文件

之前使用@ConfigurationProperties(prefix = "person")給Person注入屬性值,@ConfigurationProperties默認(rèn)從全局配置文件中獲取值,如何我們后來都把所有的配置信息都從全局的配置文件中獲取,那么這個文件就寫的太亂了。可以在resource下新建一個person.properties文件

person.last-name=九月
person.age=18
person.boss=true
person.birth=2018/10/11
person.maps.k1=v1
person.maps.k2=v2
person.lists=1,2,3
person.dog.name=dog
person.dog.age=3

使用@PropertySource(value = "{classpath:person.properties}")獲取指定文件的配置信息

@Component
@PropertySource(value?=?{"classpath:person.properties"})
public?class?Person?{
????private?String?lastName;
????private?Integer?age;
????private?Boolean?boss;
????private?Date?birth;
????private?Map<String,Object>?maps;
????private?List<Object>?lists;
????private?Dog?dog;
????//省略get/set
}

通過測試代碼看到可以獲取到指定文件的信息完成屬性映射

Person{lastName='九月',?age=18,?boss=true,?birth=Thu?Oct?11?00:00:00?CST?2018,?maps={k1=v1,?k2=v2},?lists=[1,?2,?3],?dog=Dog{name='dog',?age=3}}

@ImportResource:導(dǎo)入Spring的配置文件,讓配置文件里面的內(nèi)容生效

Spring Boot里面沒有Spring的配置文件,我們自己編寫的配置文件,也不能自動識別;
想讓Spring的配置文件生效,加載進(jìn)來;@ImportResource標(biāo)注在一個主程序配置類上

@ImportResource(value?=?{"classpath:beans.xml"})
@SpringBootApplication
public?class?DemoApplication?{

????public?static?void?main(String[]?args)?{
????????SpringApplication.run(DemoApplication.class,?args);
????}
}

可以在resource下新建一個beams.xml文件,是不是想到了之前學(xué)習(xí)SSH的配置

<?xml?version="1.0"?encoding="UTF-8"?>
<beans?xmlns="http://www.springframework.org/schema/beans"
???????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
???????xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans.xsd">
????<bean?id="helloService"?class="com.example.demo.com.jiuyue.service.HelloService"></bean>
</beans>

想讓Spring的配置文件生效,加載進(jìn)來;將@ImportResource標(biāo)注在一個配置類上就可以了


下面通過單元測試看是否將helloService注入到IOC容器中

????@Autowired
????private?ApplicationContext?ioc;
????@Test
????public?void?testHelloService(){
????????//測試判斷helloService是否在IOC容器中
????????boolean?b?=?ioc.containsBean("helloService");
????????System.out.println(b);
????}

@bean使用全注解的方式,給容器中添加組件

以前我們是在標(biāo)簽中添加組件的,SpringBoot推薦給容器中添加組件使用全注解的方式
1、建一個配置類MyAppConfig,使用@Configuration注解這個配置類,相當(dāng)于說明這個MyAppConfig是一個Sping配置文件
2、在配置類中使用@Bean注解方法,就是給容器添加組件,將方法的返回值添加到容器中;容器中這個組件默認(rèn)的id就是方法名

/**
?*?@Configuration:指明當(dāng)前類是一個配置類;就是來替代之前的Spring配置文件
?*
?*?在配置文件中用<bean><bean/>標(biāo)簽添加組件
?*
?*/
@Configuration
public?class?MyAppConfig?{

????//將方法的返回值添加到容器中;容器中這個組件默認(rèn)的id就是方法名
????@Bean
????public?HelloService?helloService02(){
????????System.out.println("配置類@Bean給容器中添加組件了...");
????????return?new?HelloService();
????}
}

單元測試:


配置文件占位符

1、隨機(jī)數(shù)

${random.value}、${random.int}、${random.long}${random.int(10)}、${random.int[1024,65536]}

2、占位符獲取之前配置的值,如果沒有可以是用:指定默認(rèn)值

person.last-name=張三${random.uuid}person.age=${random.int}person.birth=2017/12/15person.boss=falseperson.maps.k1=v1person.maps.k2=14person.lists=a,b,cperson.dog.name=${person.hello:hello}_dogperson.dog.age=15


“關(guān)注我的微信公眾號,一起進(jìn)步“


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,253評論 6 342
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • SpringMVC原理分析 Spring Boot學(xué)習(xí) 5、Hello World探究 1、POM文件 1、父項(xiàng)目...
    jack_jerry閱讀 1,476評論 0 1
  • 配置文件解析(下) 原創(chuàng)者:文思 一、yml(YAML Ain’t Markup Language)基本用法...
    文思li閱讀 2,137評論 0 2
  • 入門 介紹 Spring Boot Spring Boot 使您可以輕松地創(chuàng)建獨(dú)立的、生產(chǎn)級的基于 Spring ...
    Hsinwong閱讀 17,087評論 2 89

友情鏈接更多精彩內(nèi)容