學習兩個注解:
@PropertySource
@ImportResource ↓
@ConfigurationProperties
-
與 @Bean 結合為屬性賦值
-
與 @PropertySource (只能用於properties文件)結合讀取指定文件
:: @PropertySource : 加載指定的文件
將原來application.properties中的person的配置字段
放入單獨的person.properties中.
並在person類上加上該注解
@PropertySource(value = {"classpath:person.properties"})
public class Person {
@ImportResource 讀取外部配置文件 不推薦
導入Spring的配置文件,讓配置文件里面的內容生效.
SpringBoot里面沒有Spring的配置文件,我們自己編寫的配置文件,也不能自動識別.
想讓Spring的配置文件生效,加載進來, 需要@ImportResource標注在一個配置類上
這里放在啟動類上:
@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class Demo11Application {
public static void main(String[] args) {
SpringApplication.run(Demo11Application.class, args);
}
}
不推薦:
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloService" class="com.example.demo11.service.HelloService"></bean>
</beans>
SpringBoot推薦給容器中添加組件的方式: 推薦使用全注解的方式:
-
配置類 ===== 類似Spring配置文件
-
使用@Bean給容器中添加組件:
package com.example.demo11.config;
import com.example.demo11.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Configuration : 指明當前類是一個配置類,就是來替代之前的Spring配置文件
* 在配置文件中用<bean></bean>標簽添加組件.
*/
@Configuration
public class MyAppConfig {
//將方法的返回值添加到容器中;容器中這個組件默認的id就是方法名
@Bean
public HelloService helloService(){
System.out.println("配置類@Bean給容器添加組件了.");
return new HelloService();
}
}
>
|
>
之前的@ConfigurationProperties(prefix = "person")默認從全局配置文件中獲取值.
使用@PropertySource注解可以從自建的配置文件中獲取值,((只能用於properties文件)
新建一個person.properties文件,將原本的application.properties中person的字段剪切過去.
在bean上這樣聲明:
@PropertySource(value = {"classpath:person.properties"})
public class Person {
同樣能獲取值

Spring官方不推薦這種方式,不僅要寫xml也與SpringBoot宗旨不符合.
推薦使用一個配置類來做配置:
創建一個config包,創建一個配置類:
MyAppConfig:
package com.example.demo11.config; import com.example.demo11.service.HelloService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Configuration : 指明當前類是一個配置類,就是來替代之前的Spring配置文件 * 在配置文件中用<bean></bean>標簽添加組件. */ @Configuration public class MyAppConfig { //將方法的返回值添加到容器中;容器中這個組件默認的id就是方法名 @Bean public HelloService helloService(){ System.out.println("配置類@Bean給容器添加組件了."); return new HelloService(); } }
Profile
1.多Profile文件
我們在主配置文件編寫的時候,文件名可以是: application-{profile}.properties.yml
程序會默認使用application.properties的配置:
在默認的配置文件application.properties中,激活application-dev.properties環境
2.yml支持多文檔塊方式 ( 以 --- 隔開配置 用active激活某個文檔塊中的配置 )
多profile文檔塊模式:
application.yml :
server:
port: 8081
spring:
profiles:
# active: prod
active: dev #做為指定環境,激活哪個環境
---
server:
port: 8083
spring:
profiles: dev
---
server:
port: 8084
spring:
profiles: prod #指定屬於哪個環境
---
3.激活指定profile
-
在配置文件中指定要激活哪個配置: ( application.properties )
application.properties :
#激活環境properties
spring.profiles.active=dev
-
命令行方式激活某配置 :
除了在IDEA的Terminal也可以右鍵target, [Show in Explorer],找到jar或war包文件目錄,在路徑上覆蓋寫入cmd回車即打開該文件目錄的cmd

java -jar demoxxxxx.war --spring.profiles.active=dev

可以看到其運行端口已經換位dev的8083
-
配置IDEA的Edit Configurations設置 : ( Program arguments 程序參數 )
在IDEA的Edit Configurations設置: Program arguments 字段 :
--spring.profiles.active=dev

-
配置JVM ( java虛擬機 ) 參數 : ( VM options 虛擬機選項 )
-Dspring.profiles.active=dev

補充:
配置文件加載位置:
springboot啟動會掃描以下位置的application.properties或者application.yml文件做為springboot的默認配置文件
-
file: ../config/
-
file:../
-
classpath:/config/
-
classpath:/
以上都是按照優先級從高到底的順序,所有位置的文件都會被加載,高優先級配置內容會覆蓋低優先級配置內容.
我們也可以通過配置spring.config.location來改變默認配置
SpringBoot會從這四個位置全部加載主配置文件:
互補配置.(下面提到)
重要:補充
在整體項目中建立config文件夾: 其中的application.properties會最優先執行.
也就是在項目的最外層建立文件夾config為最優先級,即上面的file:../config/ 其次是直接不在config文件夾直接在外面建立配置文件,
這樣的配置文件都要比classpath也就是 resources 下面的優先級要高.
在有config文件夾時,如果外面classpath還有配置application.yml時,如果
只是
server:
port: 8081
那么還是會執行config文件夾下的配置.
但是如果在classpath中使用的是:
spring:
profiles:
active: dev
---
server:
port: 8083
spring:
profiles: dev
那么還是會先執行這里的yml激活的8083端口.
如果在config文件夾下有application.properties和application.yml
指定了不同端口而yml沒有激活active指定配置代碼塊,那么會讀取properties的配置.
同樣,在IDEA的Edit配置中,使用-Dspring.profiles.active=dev,如果在config和classpath中的yml
都有dev環境時,會優先指定config目錄下的.
在使用java -jar xxxx.jar --spring.profiles.active=prod 時,即使在Edit Configurations指定了-Dspring.profiles.active=dev,
但是還是會使用prod的配置.這是命令行的最優先級.
上面提到
互補配置:
即是說在config中的配置端口會執行,但是如果classpath中的配置文件多了一個配置如:
server.servlet.context-path=/boot123
那么在項目啟動后,該路徑也會加載進來.
