javascript
读取外部配置文件_SpringBoot外部配置、优先级及配置详解
一、外部配置及優(yōu)先級
SpringBoot的外部配置屬性值官方給出了很多種方式,以便可以在不同的環(huán)境中使用相同的代碼。
其使用了非常特別的PropertySource命令,旨在允許合理的覆蓋值。當然,如果屬性值不同,則這些配置方式中的屬性值都會被加載;按照從高到低的排序如下:
(1)、在您的HOME目錄設置的Devtools全局屬性(~/.spring-boot-devtools.properties)。
(2)、單元測試中的 @TestPropertySource 注解。
(3)、單元測試中的 @SpringBootTest#properties 注解屬性
(4)、命令行參數。
SPRING_APPLICATION_JSON=‘{"foo":{"bar":"spam"}}‘ java -jar myapp.jar
(6)、ServletConfig 初始化參數。
(7)、ServletContext 初始化參數。
(8)、來自 java:comp/env 的JNDI屬性。
(9)、Java系統(tǒng)屬性(System.getProperties())。
(10)、操作系統(tǒng)環(huán)境變量。
(11)、RandomValuePropertySource,只有隨機的屬性 random.* 中。
(12)、jar包外面的 Profile-specific application properties (application- {profile} .properties和YAML)
(13)、jar包內的 Profile-specific application properties (application-{profile}.properties和YAML)
(14)、jar包外的應用屬性文件(application.properties和YAML)。
(15)、jar包內的應用屬性文件(application.properties和YAML)。
(16)、在@Configuration上的@PropertySource注解。
(17)、默認屬性(使用SpringApplication.setDefaultProperties設置)。
在具體的講解這些配置的時候我們先來做一些準備工作,
(1)、書寫一個Controller
@RestController @Slf4j public class MyController {@Value("${name}")private String name;@GetMapping("/getDefaultProperties")public String getDefaultProperties() {return name;}1、使用SpringApplication.setDefaultProperties設置默認屬性
在應用程序主類main方法中在調用run方法之前,設置默認值
@SpringBootApplication public class Application {public static void main(String[] args) {//SpringApplication.run(Application.class, args);Properties properties = new Properties();properties.setProperty("name", "(17)、默認屬性(使用SpringApplication.setDefaultProperties設置)");SpringApplication application = new SpringApplication(Application.class);application.setDefaultProperties(properties);application.run(args);//new SpringApplicationBuilder()// .sources(Application.class)// .bannerMode(Banner.Mode.OFF)// .properties(properties)// .run(args);} }此時你訪問http://localhost:8080/getDefaultProperties在頁面上輸出的內容為
備注:如果你想把你項目中的所有的配置放到配置中心Apollo的話,這種方式也是可以很方法的實現的。2、在@Configuration上的@PropertySource注解
@SpringBootApplication @PropertySource(value = {"classpath:test/propertySource.properties"}, encoding = "UTF-8") public class Application {public static void main(String[] args) {Properties properties = new Properties();properties.setProperty("name", "(17)、默認屬性(使用SpringApplication.setDefaultProperties設置)");SpringApplication application = new SpringApplication(Application.class);application.setDefaultProperties(properties);application.run(args);} }3、jar包內的應用屬性文件(即默認的配置文件)
name=(15)、jar包內的應用屬性文件4、jar包外的應用屬性文件
(1)、將你的應用程序打包成可運行的jar,在jar包的同級目錄中放置一個application.properties文件,里面的內容如下:
name=(14)、jar包外的應用屬性文件5、jar包內的 Profile-specific application properties
新建application-test.properties文件,里面的內容如下
name=(13)、jar包內的 Profile-specific application properties設置啟動參數
訪問鏈接得到下圖結果
6、jar包外面的 Profile-specific application properties
(1)、將你的應用程序打包成可運行的jar,在jar包的同級目錄中放置一個application-test.properties文件,里面的內容如下:
name=(14)、jar包外的應用屬性文件(2)、java -jar -Dspring.profiles.active=test ***.jar
springboot讀取外部和內部配置文件的方法,如下優(yōu)先級:
第一種是在執(zhí)行命令的目錄下建config文件夾。(在jar包的同一目錄下建config文件夾,執(zhí)行命令需要在jar包目錄下才行),然后把配置文件放到這個文件夾下。
第二種是直接把配置文件放到jar包的同級目錄。
第三種在classpath下建一個config文件夾,然后把配置文件放進去。
第四種是在classpath下直接放配置文件。
springboot默認是優(yōu)先讀取它本身同級目錄下的一個config/application.properties 文件的。
在src/main/resources 文件夾下創(chuàng)建的application.properties 文件的優(yōu)先級是最低的
7、命令行屬性
默認情況下,SpringApplication將任何命令行選項參數(以'-- '開頭,例如--server.port=9000)轉換為屬性,并將其添加到Spring環(huán)境中。 如上所述,命令行屬性始終優(yōu)先于其他屬性來源。
如果不希望將命令行屬性添加到環(huán)境中,可以使用SpringApplication.setAddCommandLineProperties(false)禁用它們。
二、properties文件中的占位符
application.properties中的值在使用時通過已有的環(huán)境進行過濾,以便可以引用之前已經定義好的值
app.name=MyApp app.description=${app.name} is a Spring Boot application三、使用YAML替代 Properties
YAML是JSON的超集,因此這是分層配置數據一種非常方便的格式,。 每當您的類路徑中都有SnakeYAML庫時,SpringApplication類將自動支持YAML作為 properties 的替代方法。
如果您使用“Starters”,SnakeYAML將通過spring-boot-starter自動提供。1、加載yaml
Spring Framework提供了兩個方便的類,可用于加載YAML文檔。 YamlPropertiesFactoryBean將YAML作為Properties加載,YamlMapFactoryBean將YAML作為Map加載。
例如下面這個YAML文檔
environments:dev:url: http://dev.bar.comname: Developer Setupprod:url: http://foo.bar.comname: My Cool App將轉換為屬性
environments.dev.url=http://dev.bar.com environments.dev.name=Developer Setup environments.prod.url=http://foo.bar.com environments.prod.name=My Cool AppYAML列表表示為具有[index] dereferencers的屬性鍵,例如YAML:
my:servers:- dev.bar.com- foo.bar.com將轉化為屬性:
my.servers[0]=dev.bar.com my.servers[1]=foo.bar.com2、通過@ConfigurationProperties將配置綁定到Bean
(1)、配置bean的書寫
@Getter @Setter @Component @ConfigurationProperties(prefix = "my") public class MyServerProperties {private String name;private List<String> servers = new ArrayList<>(); }(2)、application.yaml文件書寫
my:servers:- dev.bar.com- foo.bar.comname: myName3、YAML的缺點
YAML文件無法通過@PropertySource注解加載。 因此,在需要以這種方式加載值的情況下,需要使用properties文件。
四、類型安全的配置屬性
使用@Value(“${property}”)注釋來注入配置屬性有時可能很麻煩(類型常規(guī)配置),特別是如果您正在使用多個層次結構的屬性或數據時。 Spring Boot提供了一種處理屬性的替代方法,允許強類型Bean管理并驗證應用程序的配置。
(1)、Bean的定義
/*** 類型安全的配置屬性** @Author YUBIN* @create 2019-06-16*/ @ConfigurationProperties("foo") public class FooProperties {private boolean enabled;private InetAddress remoteAddress;private final Security security = new Security();public boolean isEnabled() {return enabled;}public void setEnabled(boolean enabled) {this.enabled = enabled;}public InetAddress getRemoteAddress() {return remoteAddress;}public void setRemoteAddress(InetAddress remoteAddress) {this.remoteAddress = remoteAddress;}public Security getSecurity() {return security;}public static class Security {private String username;private String password;private List<String> roles = new ArrayList<>(Collections.singleton("USER"));public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public List<String> getRoles() {return roles;}public void setRoles(List<String> roles) {this.roles = roles;}} }(2)、YAML文件中的配置
foo:remote-address: 192.168.1.1security:username: fooroles:- USER- ADMIN(3)、應用程序主類上加上必要的注解
@EnableConfigurationProperties({FooProperties.class}) public class Application {這樣在程序中就可以使用@Autowired的形式注入配置類了
@RestController @Slf4j public class MyController {@Autowiredprivate FooProperties fooProperties;@GetMapping("/getFooProperties")public String getFooProperties() {return JSON.toJSONString(fooProperties);}五、第三方配置
現在我們在構建一個名為"yubin-common",類型為jar的maven項目
1、書寫一個配置類
@ConfigurationProperties(prefix = "bar") public class BarComponent {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;} }在之前的項目中引入此項目的依賴;這時如果在之前的項目中需要使用BarComponent這個屬性類的話,則需要通過@EnableConfigurationProperties(BarComponent.class)這個注解來引入類,但是這樣做是否合理呢?一個項目這么做,當有其它的項目也需要這個類的話是不是也要這么做呢?
public class BarComponent {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;} } @Configuration public class ConfigurationBean {@Bean@ConfigurationProperties(prefix = "bar")public BarComponent barComponent() {return new BarComponent();} }2、小結
@ConfigurationProperties導入外部屬性填充到這個Bean的實例,有三種方式:
(1)、@ConfigurationProperties + @Component 注解到bean定義類上
(2)、@ConfigurationProperties + @Bean注解在配置類的bean定義方法上
(3)、@ConfigurationProperties注解到普通類然后通過@EnableConfigurationProperties定義為bean
六、寬松的綁定
Spring Boot使用一些寬松的規(guī)則將環(huán)境屬性綁定到@ConfigurationProperties bean,因此不需要在Environment屬性名稱和bean屬性名稱之間進行完全匹配。 常用的例子是這樣有用的:虛分離(例如上下文路徑綁定到contextPath)和大寫(例如PORT綁定到端口)環(huán)境屬性。
配置bean
/*** 屬性bean寬松綁定演示** @Author YUBIN* @create 2019-06-16*/ @Component @ConfigurationProperties(prefix = "person") @Getter @Setter public class OwnerProperties {private String firstName; //person.firstName 標準駱峰命名法。private String secondName; // person.second-name 虛線符號,推薦用于.properties和.yml文件。private String thirdName; // person.third_name 下劃線符號,用于.properties和.yml文件的替代格式。private String fourName; // PERSON_FOUR_NAME 大寫格式 推薦使用系統(tǒng)環(huán)境變量時。 }測試類
@RestController @Slf4j public class MyController {@Autowiredprivate OwnerProperties ownerProperties;@GetMapping("/getOwnerProperties")public String getOwnerProperties() {return JSON.toJSONString(ownerProperties);} }七、@ConfigurationProperties驗證
如果你需要驗證@ConfigurationProperties類。 您可以直接在配置類上使用JSR-303 javax.validation約束注解 @Validated。 只需確保您的類路徑中符合JSR-303實現,然后在您的字段中添加約束注釋:
@ConfigurationProperties("foo") @Validated public class FooProperties {private boolean enabled;@NotNullprivate InetAddress remoteAddress; // ... getters and setters為了驗證嵌套屬性的值,您必須將關聯字段注釋為@Valid以觸發(fā)其驗證。 例如,基于上述FooProperties示例:
@ConfigurationProperties(prefix="connection") @Validated public class FooProperties {@NotNullprivate InetAddress remoteAddress;@Validprivate final Security security = new Security();// ... getters and setterspublic static class Security {@NotEmptypublic String username;// ... getters and setters} }@ConfigurationProperties 對比 @Value
@Value是核心容器功能,它不提供與類型安全配置屬性相同的功能。 下表總結了@ConfigurationProperties和@Value支持的功能:
八、配置文件Profiles
1、代碼層面區(qū)分環(huán)境
Spring 配置文件提供了將應用程序配置隔離的方法,使其僅在某些環(huán)境中可用。 任何@Component或@Configuration都可以使用@Profile進行標記,以限制其在什么時候加載:
(1)、測試環(huán)境
@Service @Profile({"test"}) public class TestProfileServiceImpl implements ProfileService {@Overridepublic String getEnvironment() {return "test環(huán)境";} }(2)、dev環(huán)境
@Service @Profile({"dev"}) public class DevProfileServiceImpl implements ProfileService {@Value("${spring.profiles.active}")private String profileActive;@Overridepublic String getEnvironment() {return "dev環(huán)境";} }(3)、測試類
@RestController @Slf4j public class MyController {@Autowiredprivate ProfileService profileService;@GetMapping("/getProfileService")public String getProfileService() {return profileService.getEnvironment();} }當設置啟動參數為 spring.profiles.active=dev時,頁面展示效果:
image.png
當設置啟動參數為 spring.profiles.active=test時,頁面展示效果:
總結
以上是生活随笔為你收集整理的读取外部配置文件_SpringBoot外部配置、优先级及配置详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 智慧交通day01-算法库03:cv.d
- 下一篇: gradle idea java ssm