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