SpringBoot項目加載配置文件的6種方式小結(jié)
1、通過@value注入
滿足條件:
1、該類首先要被SpringBoot框架管理,屬于SpringBoot框架的Bean。
2、application.properties(或者application.yml)配置文件中存在該配置名。(如果不存在可以加冒號,框架會賦值默認(rèn)值,也可以在冒號后面自定義默認(rèn)值。例如:test.domain:)。配置文件中不包含該配置,注解中也沒加冒號,項目啟動就會報錯。
3、被static和finely修飾的屬性配置該注解不會生效。
@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)行自動裝配屬性。適用于批量綁定,批量注入屬性值。相比@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、通過框架自帶對象Environment實現(xiàn)屬性動態(tài)注入
#application.yml配置文件 es: post:9200 host:localhost name:es
方式一:容器自動注入SpringBoot框架自帶的類Environment進(jìn)行實現(xiàn)動態(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); } }
方式二:自己實現(xiàn)EnvironmentAware接口進(jìn)行重寫方法進(jìn)行注入Environment。好處可以和spring boot框架的解耦性更低一點。
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注解實現(xiàn)外部配置文件注入屬性值
#es.properties配置文件 es.post=9200 es.host=localhost es.name=es
1、通過@PropertySource注解實現(xiàn)導(dǎo)入外部配置文件。
2、配合@value注解實現(xiàn)屬性注入或者@ConfigurationProperties注解實現(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 外部配置文件動態(tài)注入
第4中方式只能實現(xiàn) .properties 文件的,該方式是實現(xiàn) .yml 文件的。
1、自定義配置類,實例化PropertySourcesPlaceholderConfigurer類,使用該類進(jìn)行屬性值的注入。
2、實例化完P(guān)ropertySourcesPlaceholderConfigurer類之后,就可以配合@value注解實現(xiàn)屬性注入或者@ConfigurationProperties注解實現(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項目加載配置文件的6種方式小結(jié)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot加載配置文件的資料請關(guān)注腳本之家其它相關(guān)文章!
- springboot無法加載yml配置文件的解決方案
- SpringBoot使用不同環(huán)境動態(tài)加載不同配置文件
- SpringBoot配置文件啟動加載順序的方法步驟
- SpringBoot配置文件的優(yōu)先級順序、加載順序、bootstrap.yml與application.yml區(qū)別及說明
- SpringBoot項目部署時application.yml文件的加載優(yōu)先級和啟動腳本問題
- SpringBoot中的配置文件加載優(yōu)先級詳解
- SpringBoot加載不出來application.yml文件的解決方法
- SpringBoot實現(xiàn)配置文件自動加載和刷新的示例詳解
- SpringBoot的配置文件application.yml及加載順序詳解
- springboot加載配值文件的實現(xiàn)步驟
相關(guān)文章
通過netty把百度地圖API獲取的地理位置從Android端發(fā)送到Java服務(wù)器端的操作方法
這篇文章主要介紹了通過netty把百度地圖API獲取的地理位置從Android端發(fā)送到Java服務(wù)器端,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10Java讀寫鎖ReadWriteLock原理與應(yīng)用場景詳解
這篇文章主要介紹了Java讀寫鎖ReadWriteLock原理與應(yīng)用場景詳解,讀寫狀態(tài)的設(shè)計,寫鎖的獲取與釋放,鎖降級需要的朋友可以參考下2023-02-02java使用Feign實現(xiàn)聲明式Restful風(fēng)格調(diào)用
這篇文章主要為大家詳細(xì)介紹了java使用Feign實現(xiàn)聲明式Restful風(fēng)格調(diào)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04Java中StringBuilder常用構(gòu)造方法解析
這篇文章主要介紹了Java中StringBuilder常用構(gòu)造方法解析,StringBuilder是一個可標(biāo)的字符串類,我們可以吧它看成是一個容器這里的可變指的是StringBuilder對象中的內(nèi)容是可變的,需要的朋友可以參考下2024-01-01