SpringBoot項(xiàng)目加載配置文件的6種方式小結(jié)
1、通過@value注入
滿足條件:
1、該類首先要被SpringBoot框架管理,屬于SpringBoot框架的Bean。
2、application.properties(或者application.yml)配置文件中存在該配置名。(如果不存在可以加冒號(hào),框架會(huì)賦值默認(rèn)值,也可以在冒號(hào)后面自定義默認(rèn)值。例如:test.domain:)。配置文件中不包含該配置,注解中也沒加冒號(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、通過@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、通過框架自帶對(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)行重寫方法進(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、通過@PropertySource注解實(shí)現(xiàn)外部配置文件注入屬性值
#es.properties配置文件 es.post=9200 es.host=localhost es.name=es
1、通過@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無法加載yml配置文件的解決方案
- SpringBoot使用不同環(huán)境動(dòng)態(tài)加載不同配置文件
- SpringBoot配置文件啟動(dòng)加載順序的方法步驟
- SpringBoot配置文件的優(yōu)先級(jí)順序、加載順序、bootstrap.yml與application.yml區(qū)別及說明
- SpringBoot項(xiàng)目部署時(shí)application.yml文件的加載優(yōu)先級(jí)和啟動(dòng)腳本問題
- SpringBoot中的配置文件加載優(yōu)先級(jí)詳解
- SpringBoot加載不出來application.yml文件的解決方法
- SpringBoot實(shí)現(xiàn)配置文件自動(dòng)加載和刷新的示例詳解
- SpringBoot的配置文件application.yml及加載順序詳解
- springboot加載配值文件的實(shí)現(xiàn)步驟
相關(guān)文章
通過netty把百度地圖API獲取的地理位置從Android端發(fā)送到Java服務(wù)器端的操作方法
這篇文章主要介紹了通過netty把百度地圖API獲取的地理位置從Android端發(fā)送到Java服務(wù)器端,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
Java讀寫鎖ReadWriteLock原理與應(yīng)用場(chǎng)景詳解
這篇文章主要介紹了Java讀寫鎖ReadWriteLock原理與應(yīng)用場(chǎng)景詳解,讀寫狀態(tài)的設(shè)計(jì),寫鎖的獲取與釋放,鎖降級(jí)需要的朋友可以參考下2023-02-02
java使用Feign實(shí)現(xiàn)聲明式Restful風(fēng)格調(diào)用
這篇文章主要為大家詳細(xì)介紹了java使用Feign實(shí)現(xiàn)聲明式Restful風(fēng)格調(diào)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
如何設(shè)置springboot禁止日志輸出到控制臺(tái)
文章總結(jié):本文主要介紹了SpringBoot項(xiàng)目中使用SLF4J記錄日志時(shí),日志默認(rèn)輸出到控制臺(tái)的原因及解決方法,日志框架如Logback默認(rèn)會(huì)將日志輸出到控制臺(tái),可以通過`logback-spring.xml`配置文件或配置類來禁止日志輸出到控制臺(tái),并設(shè)置日志輸出級(jí)別2025-01-01
詳解用Eclipse如何創(chuàng)建Web項(xiàng)目
本篇文章主要介紹了詳解用Eclipse如何創(chuàng)建Web項(xiàng)目,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
基于Java實(shí)現(xiàn)XML文件的解析與更新
配置文件可以有很多種格式,包括?INI、JSON、YAML?和?XML。每一種編程語(yǔ)言解析這些格式的方式都不同。本文將通過Java語(yǔ)言實(shí)現(xiàn)XML文件的解析與更新,需要的可以參考一下2022-03-03
Java中StringBuilder常用構(gòu)造方法解析
這篇文章主要介紹了Java中StringBuilder常用構(gòu)造方法解析,StringBuilder是一個(gè)可標(biāo)的字符串類,我們可以吧它看成是一個(gè)容器這里的可變指的是StringBuilder對(duì)象中的內(nèi)容是可變的,需要的朋友可以參考下2024-01-01

