SpringBoot項(xiàng)目加載配置文件的6種方式小結(jié)
1、通過(guò)@value注入
滿足條件:
1、該類首先要被SpringBoot框架管理,屬于SpringBoot框架的Bean。
2、application.properties(或者application.yml)配置文件中存在該配置名。(如果不存在可以加冒號(hào),框架會(huì)賦值默認(rèn)值,也可以在冒號(hào)后面自定義默認(rèn)值。例如:test.domain:)。配置文件中不包含該配置,注解中也沒(méi)加冒號(hào),項(xiàng)目啟動(dòng)就會(huì)報(bào)錯(cuò)。
3、被static和finely修飾的屬性配置該注解不會(huì)生效。
@Component
public class BaseConfig {
@Value("${test.domain:}")
private String domamin;
@Value("${test.api:}")
private String api;
}2、通過(guò)@ConfigurationProperties注入
1、該類首先要被SpringBoot框架管理,屬于SpringBoot框架的Bean。
2、@ConfigurationProperties進(jìn)行指定配置文件中key的前綴。進(jìn)行自動(dòng)裝配屬性。適用于批量綁定,批量注入屬性值。相比@value更省事。
#application.yml配置文件 es: post:9200 host:localhost name:es
@Data
@Component
@ConfigurationProperties(prefix="es")
public class ESProperties {
private String host;
private String name;
private int port;
}3、通過(guò)框架自帶對(duì)象Environment實(shí)現(xiàn)屬性動(dòng)態(tài)注入
#application.yml配置文件 es: post:9200 host:localhost name:es
方式一:容器自動(dòng)注入SpringBoot框架自帶的類Environment進(jìn)行實(shí)現(xiàn)動(dòng)態(tài)配置屬性值注入。
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class EsProperties {
private String host;
private String name;
private int post;
@Resource
private Environment environment;
public String getHost() {
return environment.getProperty("es.host");
}
public String getName() {
return environment.getProperty("es.name");
}
public int getPost() {
String post = environment.getProperty("es.post");
return Integer.parseInt(post);
}
}方式二:自己實(shí)現(xiàn)EnvironmentAware接口進(jìn)行重寫(xiě)方法進(jìn)行注入Environment。好處可以和spring boot框架的解耦性更低一點(diǎn)。
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class EsProperties implements EnvironmentAware {
private String host;
private String name;
private int post;
private Environment environment;
public String getHost() {
return environment.getProperty("es.host");
}
public String getName() {
return environment.getProperty("es.name");
}
public int getPost() {
String post = environment.getProperty("es.post");
return Integer.parseInt(post);
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}4、通過(guò)@PropertySource注解實(shí)現(xiàn)外部配置文件注入屬性值
#es.properties配置文件 es.post=9200 es.host=localhost es.name=es
1、通過(guò)@PropertySource注解實(shí)現(xiàn)導(dǎo)入外部配置文件。
2、配合@value注解實(shí)現(xiàn)屬性注入或者@ConfigurationProperties注解實(shí)現(xiàn)批量注入。
3、該方式只能獲取 .properties 的配置文件不能獲取 .yml 的配置文件。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = "classpath:es.properties",encoding = "utf-8")
public class EsProperties {
@Value("${es.host}")
private String host;
@Value("${es.name}")
private String name;
@Value("${es.post}")
private int post;
}import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = "classpath:es.properties",encoding = "utf-8")
@ConfigurationProperties(prefix = "es")
public class EsProperties {
private String host;
private String name;
private int post;
}5、yml 外部配置文件動(dòng)態(tài)注入
第4中方式只能實(shí)現(xiàn) .properties 文件的,該方式是實(shí)現(xiàn) .yml 文件的。
1、自定義配置類,實(shí)例化PropertySourcesPlaceholderConfigurer類,使用該類進(jìn)行屬性值的注入。
2、實(shí)例化完P(guān)ropertySourcesPlaceholderConfigurer類之后,就可以配合@value注解實(shí)現(xiàn)屬性注入或者@ConfigurationProperties注解實(shí)現(xiàn)批量注入。
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import java.util.Objects;
@Configuration
public class MyYmlConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer yamlConfigurer(){
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("es.yml"));
configurer.setProperties(Objects.requireNonNull(yaml.getObject()));
return configurer;
}
}import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class EsProperties {
@Value("${es.host}")
private String host;
@Value("${es.name}")
private String name;
@Value("${es.post}")
private int post;
}import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "es")
public class EsProperties {
private String host;
private String name;
private int post;
}6、Java原生態(tài)方式注入屬性值
#es.properties配置文件 es.post=9200 es.host=localhost es.name=es
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
@Component
public class EsProperties {
private String host;
private String name;
private int post;
@Bean
public void init(){
Properties props = new Properties();
InputStreamReader inputStreamReader = new InputStreamReader(
this.getClass().getClassLoader().getResourceAsStream("es.properties"), StandardCharsets.UTF_8);
try {
props.load(inputStreamReader);
} catch (IOException e) {
System.out.println(e.getMessage());
}
this.host = props.getProperty("es.host");
this.name = props.getProperty("es.name");
this.post = Integer.parseInt(props.getProperty("es.post"));
}
}以上就是SpringBoot項(xiàng)目加載配置文件的6種方式小結(jié)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot加載配置文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- springboot無(wú)法加載yml配置文件的解決方案
- SpringBoot使用不同環(huán)境動(dòng)態(tài)加載不同配置文件
- SpringBoot配置文件啟動(dòng)加載順序的方法步驟
- SpringBoot配置文件的優(yōu)先級(jí)順序、加載順序、bootstrap.yml與application.yml區(qū)別及說(shuō)明
- SpringBoot項(xiàng)目部署時(shí)application.yml文件的加載優(yōu)先級(jí)和啟動(dòng)腳本問(wèn)題
- SpringBoot中的配置文件加載優(yōu)先級(jí)詳解
- SpringBoot加載不出來(lái)application.yml文件的解決方法
- SpringBoot實(shí)現(xiàn)配置文件自動(dòng)加載和刷新的示例詳解
- SpringBoot的配置文件application.yml及加載順序詳解
- springboot加載配值文件的實(shí)現(xiàn)步驟
相關(guān)文章
JSON的String字符串與Java的List列表對(duì)象的相互轉(zhuǎn)換
這篇文章主要介紹了JSON的String字符串與Java的List列表對(duì)象的相互轉(zhuǎn)換,如果在瀏覽器端JSON是list則轉(zhuǎn)為string結(jié)構(gòu)來(lái)處理,需要的朋友可以參考下2016-04-04
避免多個(gè)jar通過(guò)maven打包導(dǎo)致同名配置文件覆蓋沖突問(wèn)題
這篇文章主要介紹了避免多個(gè)jar通過(guò)maven打包導(dǎo)致同名配置文件覆蓋沖突問(wèn)題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Java數(shù)據(jù)結(jié)構(gòu)之二叉查找樹(shù)的實(shí)現(xiàn)
二叉查找樹(shù)(亦稱二叉搜索樹(shù)、二叉排序樹(shù))是一棵二叉樹(shù),且各結(jié)點(diǎn)關(guān)鍵詞互異,其中根序列按其關(guān)鍵詞遞增排列。本文將通過(guò)示例詳細(xì)講解二叉查找樹(shù),感興趣的可以了解一下2022-03-03
IDEA中創(chuàng)建maven項(xiàng)目引入相關(guān)依賴無(wú)法下載jar問(wèn)題及解決方案
這篇文章主要介紹了IDEA中創(chuàng)建maven項(xiàng)目引入相關(guān)依賴無(wú)法下載jar問(wèn)題及解決方案,本文通過(guò)圖文并茂的形式給大家分享解決方案,需要的朋友可以參考下2020-07-07
教你一步解決java.io.FileNotFoundException:找不到文件異常
這篇文章主要給大家介紹了關(guān)于如何一步解決java.io.FileNotFoundException:找不到文件異常的相關(guān)資料,文中通過(guò)圖文以及代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
SpringBoot集成SpringSecurity安全框架方式
這篇文章主要介紹了SpringBoot集成SpringSecurity安全框架方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
GSON實(shí)現(xiàn)Java對(duì)象的JSON序列化與反序列化的實(shí)例教程
GSON是Google開(kāi)發(fā)并開(kāi)源的一個(gè)Java的JSON轉(zhuǎn)換庫(kù),這里我們將來(lái)講解GSON實(shí)現(xiàn)Java對(duì)象的JSON序列化與反序列化的實(shí)例教程,需要的朋友可以參考下2016-06-06

