spring默認(rèn)只用兩種配置文件:
SpringBoot使用一個(gè)全局的配置文件,配置文件名是固定的;
*application.properties
*application.yml
配置文件的作用:
修改SpringBoot自動(dòng)配置的默認(rèn)值;SpringBoot在底層都給我們自動(dòng)配置好;
有以下說(shuō)法:
標(biāo)記語(yǔ)言:
以前的配置文件;大多都使用的是 xxxx.xml文件;
YAML:以數(shù)據(jù)為中心,比json、xml等更適合做配置文件;
YAML:配置例子:
server: port: 8082
properties配置:
server.port=8081
xml配置:
<server><port>8081</port> </server>
springboot使用properties或yml均可,選一即可。
k:(空格)v
:表示一對(duì)鍵值對(duì)(空格必須有);
以空格的縮進(jìn)來(lái)控制層級(jí)關(guān)系;
只要是左對(duì)齊的一列數(shù)據(jù),都是同一個(gè)層級(jí)的
server:port: 8081path: /hello
屬性和值也是大小寫(xiě)敏感;
屬性名匹配規(guī)則(Relaxedbinding)
person.firstName
:使用標(biāo)準(zhǔn)方式person.first-name
:大寫(xiě)用-person.first_name
:大寫(xiě)用_PERSON_FIRST_NAME
: 推薦系統(tǒng)屬性使用這種寫(xiě)法方式1:
k: v:在下一行來(lái)寫(xiě)對(duì)象的屬性和值的關(guān)系;注意縮進(jìn)
對(duì)象還是k: v的方式
friends: lastName: zhangsan age: 20
方式2:行內(nèi)寫(xiě)法
friend: {lastName: z, age: 12 }
用- 值表示數(shù)組中的一個(gè)元素
pets: - dog - cat - pig
pet: [dog,cat,pig]
1.Person.java
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; ...
2.配置文件application.yml
person: lastName: hello age: 18 boss: false birth: 2017/12/12 maps: {k1: v1,k2: 12} lists: ‐ lisi ‐ zhaoliu dog: name: 小狗 age: 12
3.在bean需要加上注解,才能讀配置文件
/** * 將配置文件中配置的每一個(gè)屬性的值,映射到這個(gè)組件中 * 1: * @ConfigurationProperties: * 告訴SpringBoot將本類(lèi)中的所有屬性和配置文件中相關(guān)的配置進(jìn)行綁定; * prefix = "person":配置文件中哪個(gè)下面的所有屬性進(jìn)行一一映射 * * 2: * @Component: * 只有這個(gè)組件是容器中的組件,才能容器提供的@ConfigurationProperties功能; * */
@Component@ConfigurationProperties(prefix = "person")public class Person {
<!--導(dǎo)入配置文件處理器,配置文件進(jìn)行綁定就會(huì)有提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
這樣寫(xiě)配置就會(huì)提示了:
person: last-name: wang #這種寫(xiě)法和lastName都可以
4.單元測(cè)試
@RunWith(SpringRunner.class)@SpringBootTestpublic class SpringBoot01SpringworldQuickApplicationTests { @Autowired Person person; @Test public void contextLoads() { System.out.println(person); } ... //結(jié)果 Person{lastName='hello', age=18, boss=false, birth=Tue Dec 12 00:00:00 CST 2017, maps={k1=v1, k2=12}, lists=[‐ lisi ‐ zhaoliu], dog=Dog{name='小狗', age=12}}
將application.yml中的內(nèi)容注釋掉,將配置寫(xiě)在application.properties中也行。
server.port=8081# idea,properties配置文件utf-8person.last-name=張三person.age=12person.birth=2017/12/15person.boss=falseperson.maps.k1=v1person.maps.k2=14person.lists=a,b,c
測(cè)試結(jié)果:
Person{lastName='?? ??‰', age=12, boss=false, birth=Fri Dec 15 00:00:00 CST 2017, maps={k1=v1, k2=14}, lists=[a, b, c], dog=null}
不使用@ConfigurationProperties,對(duì)每個(gè)屬性均時(shí)候用@Value注解也可以。
@Value方式:
@Value是底層注解,同xml中配置bean的方式。 /** * <bean class="Person"> * <property name="lastName" value="①字面量/②${key}從環(huán)境變量、配置文件中獲取值/③#{SpEL}"></property> * <bean/> */
@Component//@ConfigurationProperties(prefix = "person")public class Person { @Value("${person.last-name}") //讀配置 private String lastName; @Value("#{11*2}") //表達(dá)式 private Integer age; @Value("true") //字面量 private Boolean boss;
@Component@ConfigurationProperties(prefix = "person")@Validatedpublic class Person { @Email private String lastName;
上述代碼即可校驗(yàn)lastName
對(duì)比:
配置文件yml還是properties他們都能獲取到值;
如果說(shuō),我們只是在某個(gè)業(yè)務(wù)邏輯中需要獲取一下配置文件中的某項(xiàng)值,使用@Value
;
如果說(shuō),我們專(zhuān)門(mén)編寫(xiě)了一個(gè)javaBean來(lái)和配置文件進(jìn)行映射,我們就直接使用@Con?gurationProperties
;
@ConfigurationProperties(prefix = “person”) 默認(rèn)從全局配置文件中獲取值。
以下注解可以指定配置
@ConfigurationProperties(prefix = "person")@PropertySource(value = {"classpath:person.properties"})public class Person {
說(shuō)明:雖然使用@PropertySource(value = {“classpath:person.properties”})指出了文件位置。還是要@ConfigurationProperties(prefix = “person”)注解。
Spring Boot里面沒(méi)有Spring的配置xml文件,我們自己編寫(xiě)的配置文件,也不能自動(dòng)識(shí)別; 想讓Spring的配置文件生效,加載進(jìn)來(lái);需要將@ImportResource標(biāo)注在一個(gè)配置類(lèi)上。
beans.xml
:
<?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="cn.whbing.springboot01.service.HelloService"> </bean></beans>
上述beans是想將HelloService注入到容器。如果直接建配置文件不會(huì)生效,需要在主配置類(lèi)上加注解。
主配置加上@ImportResource注解:
@ImportResource(locations = {"classpath:beans.xml"})導(dǎo)入Spring的配置文件讓其生效
@ImportResource(locations = {"classpath:beans.xml"}) //加載xml配置@SpringBootApplicationpublic class HelloworldQuickApplication { public static void main(String[] args) { SpringApplication.run(HelloworldQuickApplication.class, args); }}
測(cè)試:
@RunWith(SpringRunner.class)@SpringBootTestpublic class SpringBoot01SpringworldQuickApplicationTests { @Autowired Person person; @Autowired ApplicationContext ioc; //上下文 @Test public void contextLoads() { System.out.println(person); } @Test public void helloServiceTest(){ boolean b = ioc.containsBean("helloService"); System.out.println(b); //true }}
springboot不推薦xml配置文件,推薦全注解方式。
過(guò)程:
(1)配置類(lèi)@Configuration---->Spring xml配置
(2)@Bean給容器中添加組件
/** * @Configuration:指明當(dāng)前類(lèi)是一個(gè)配置類(lèi);就是來(lái)替代之前的Spring配置文件 * * 在配置文件中用<bean><bean/>標(biāo)簽添加組件 * */@Configurationpublic class MyAppConfig { //將方法的返回值添加到容器中;容器中這個(gè)組件默認(rèn)的id就是方法名 //顯示寫(xiě)出就是用寫(xiě)出的 //@Bean @Bean("helloService02") public HelloService helloService01(){ return new HelloService(); }}
測(cè)試:
@RunWith(SpringRunner.class)@SpringBootTestpublic class SpringBoot01SpringworldQuickApplicationTests { @Autowired Person person; @Autowired ApplicationContext ioc; @Test public void contextLoads() { System.out.println(person); } @Test public void helloServiceTest(){// boolean b = ioc.containsBean("helloService");// boolean b = ioc.containsBean("helloService01"); boolean b = ioc.containsBean("helloService02"); System.out.println(b); //true }}
1、隨機(jī)數(shù)
${random.value}、${random.int}、${random.long}${random.int(10)}、${random.int[1024,65536]}
2、占位符獲取之前配置的值,如果沒(méi)有可以是用:指定默認(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
上述變量person.hello沒(méi)有找到,所以使用默認(rèn)值hello再拼接后邊的
項(xiàng)目的三個(gè)配置文件,其中,application.yml
是啟動(dòng)服務(wù)時(shí),服務(wù)器會(huì)自動(dòng)加載的配置文件,而application-dev.yml
代表的是開(kāi)發(fā)環(huán)境的配置文件,application-prod.yml
代表的是生產(chǎn)環(huán)境的配置文件,后兩個(gè)文件在啟動(dòng)服務(wù)時(shí),服務(wù)器不會(huì)自動(dòng)加載,那么在不同的環(huán)境中時(shí)怎么調(diào)用不同的文件的呢?
我們?cè)谥髋渲梦募帉?xiě)的時(shí)候,文件名可以是 application-{pro?le}.properties/yml
1、方式1:寫(xiě)多個(gè).yml的配置文件,然后在application.yml中激活。見(jiàn)springboot多配置
2、方式2:yml支持多文檔塊方式
server: port: 8081spring: profiles: active: prod‐‐‐server: port: 8083spring: profiles: dev‐‐‐server: port: 8084 spring:profiles: prod #指定屬于哪個(gè)環(huán)境
1、在配置文件中指定 spring.pro?les.active=dev
2、命令行:
java -jar spring-boot-02-con?g-0.0.1-SNAPSHOT.jar --spring.pro?les.active=dev;
可以直接在測(cè)試的時(shí)候,配置傳入命令行參數(shù)
3、虛擬機(jī)參數(shù);
-Dspring.pro?les.active=dev
springboot 啟動(dòng)會(huì)掃描以下位置的application.properties
或者application.yml
文件作為Spring boot的默認(rèn)配置文件
–?le:./con?g/–?le:./–classpath:/con?g/–classpath:/
SpringBoot會(huì)從這四個(gè)位置全部加載主配置文件;互補(bǔ)配置;
我們還可以通過(guò)spring.con?g.location來(lái)改變默認(rèn)的配置文件位置
項(xiàng)目打包好以后,我們可以使用命令行參數(shù)的形式,啟動(dòng)項(xiàng)目的時(shí)候來(lái)指定配置文件的新位置;指定配置文件和默 認(rèn)加載的這些配置文件共同起作用形成互補(bǔ)配置;
java -jar spring-boot-02-con?g-02-0.0.1-SNAPSHOT.jar --spring.con?g.location=G:/application.properties
server: context-path: /boot
則訪(fǎng)問(wèn)時(shí),都要加/boot
http://localhost:8081/boot/hello
SpringBoot也可以從以下位置加載配置; 優(yōu)先級(jí)從高到低;高優(yōu)先級(jí)的配置覆蓋低優(yōu)先級(jí)的配置,所有的配置會(huì)形成互補(bǔ)配置
如:
1、命令行參數(shù)
所有的配置都可以在命令行上進(jìn)行指定
java -jar spring-boot-02-con?g-02-0.0.1-SNAPSHOT.jar --server.port=8087 --server.context-path=/abc
多個(gè)配置用空格分開(kāi); --配置項(xiàng)=值
聯(lián)系客服