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