SpringBoot讀取多環(huán)境配置文件的幾種方式
Properties配置文件注入
如果我們還在使用相對較老的Properties文件的方式,存放我們項目的配置信息,那么我們可以通過*@PropertySource*注解、配置@Value的方式讀取我們resource目錄下的配置文件;
//applicaiton.properties配置 spring.redis.host: 127.0.0.1 spring.redis.port: 3306 spring.redis.password :123456
@Component @PropertySources(value = "classpath:application.properties") public class RedisProperties { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private String port; @Value("${spring.redis.password}") private int password; }
如果不想寫@Value注解,可以在類頭加@ConfigurationProperties(prefix = “spring.redis”)注解;
不同環(huán)境文件的讀取
Spring boot項目是優(yōu)先讀取 application.yml 或者 application.properties,profile機(jī)制會根據(jù)不同環(huán)境讀取不同的配置文件
//需要我們在applicaiton.properties設(shè)置環(huán)境變量 spring.profiles.actives = dev
Yml的方式注入
如果使用yml的方式配置文件,如何實現(xiàn)動態(tài)注入呢?
//application.yml spring: redis: host: 127.0.0.1 port: 6379 password: password
YamlPropertiesFactoryBean類讀取
可以采用Spring beans包下對yml文件配置讀取的類,YamlPropertiesFactoryBean,還有一個YamlMapFactoryBean返回map集合類型的配置;
@GetMapping("getRedisConfig") public Properties getRedisConfig(){ YamlPropertiesFactoryBean yamlProFb = new YamlPropertiesFactoryBean(); yamlProFb.setResources(new ClassPathResource("redis-file.yml")); Properties properties = yamlProFb.getObject(); System.out.println(properties.get("redis.host")); System.out.println(properties.get("redis.port")); System.out.println(properties.toString()); return properties; }
PropertySourcesPlaceholderConfigurer
也可以采用PropertySourcesPlaceholderConfigurer自定義配置類,使用該類進(jìn)入屬性注入,這個類主要解決的是YamlPropertiesFactoryBean只限于在一個接口使用,如果在定義一個接口不用YamlPropertiesFactoryBean去加載配置類,是無法讀取的; 所以我們用PropertySourcesPlaceholderConfigurer,該類是實現(xiàn)Aware接口,而最根本還是調(diào)BeanFactoryPostProcessor,bean工廠的后置處理器;
所以我們這么配置,就可以實現(xiàn)全局的配置文件屬性讀??;
@Configuration public class YmlRedisConfig { @Bean public static PropertySourcesPlaceholderConfigurer yamlConfigurer(){ PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean(); yaml.setResources(new ClassPathResource("redis-file.yml")); configurer.setProperties(Objects.requireNonNull(yaml.getObject())); return configurer; } }
Environment實現(xiàn)屬性動態(tài)注入
Spring boot在啟動的時候,會構(gòu)建一個org.springframework.core.env.Environment對象,用于存儲配置類; 根據(jù)下圖,我們可以看到最終都會去實現(xiàn)PropertyResolver接口;
[image:2C39516B-C70F-485E-9F88-A4A7CA7F4292-2581-00001ED1E5808FF4/4CE2B56C-5B64-4756-B38A-18BA819200E1.png]
@Autowired private Environment env; @Bean public DataSource dataSource() throws PropertyVetoException, SQLException { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(env.getProperty("mysql.connection.url")); dataSource.setUsername(env.getProperty("mysql.connection.username")); dataSource.setPassword(env.getProperty("mysql.connection.password")); return dataSource; }
輸入、輸出流原生態(tài)注入
InputStreamReader字節(jié)流讀取application.propertie,Properties.load()方法 進(jìn)行讀??;
@Bean public Properties initRedisConfig() throws IOException { Properties properties =new Properties(); InputStreamReader inputStreamReader =new InputStreamReader( this.getClass().getResourceAsStream("application.properties"), StandardCharsets.UTF_8); properties.load(inputStreamReader); return properties; }
到此這篇關(guān)于SpringBoot讀取多環(huán)境配置文件的幾種方式的文章就介紹到這了,更多相關(guān)SpringBoot讀取多環(huán)境配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用Jasypt對配置文件和數(shù)據(jù)庫密碼加密
- springboot中非容器類如何獲取配置文件數(shù)據(jù)
- 詳解SpringBoot依賴注入和使用配置文件
- SpringBoot如何從配置文件中讀取配置參數(shù)
- SpringBoot中的配置文件加載優(yōu)先級詳解
- Springboot如何實現(xiàn)對配置文件中的明文密碼加密
- SpringBoot中的YAML配置文件和日志詳解
- SpringBoot實現(xiàn)配置文件加密的方案分享
- SpringBoot綁定配置文件中變量的四種方式總結(jié)
- SpringBoot中獲取配置文件的注解詳解
- Spring Boot 配置文件(application.yml、application-dev.yml、application-test.yml)
相關(guān)文章
Java利用自定義注解實現(xiàn)數(shù)據(jù)校驗
JSR303是一套JavaBean參數(shù)校驗的標(biāo)準(zhǔn),它提供了一系列的校驗方式,這些校驗方式在javax.validation.constraints包中。本文就來聊聊如何利用它實現(xiàn)數(shù)據(jù)校驗2022-09-09Java代碼如何判斷l(xiāng)inux系統(tǒng)windows系統(tǒng)
這篇文章主要介紹了Java代碼如何判斷l(xiāng)inux系統(tǒng)windows系統(tǒng)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01SpringBoot整合MybatisPlus實現(xiàn)增刪改查功能
MybatisPlus是國產(chǎn)的第三方插件,?它封裝了許多常用的CURDapi,免去了我們寫mapper.xml的重復(fù)勞動。本文將整合MybatisPlus實現(xiàn)增刪改查功能,感興趣的可以了解一下2022-05-05SpringBoot之使用Redis實現(xiàn)分布式鎖(秒殺系統(tǒng))
這篇文章主要介紹了SpringBoot之使用Redis實現(xiàn)分布式鎖(秒殺系統(tǒng)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04深入解析Java多態(tài)進(jìn)階學(xué)習(xí)
java的動態(tài)綁定機(jī)制非常重要。這篇文章將帶大家更深入的學(xué)習(xí)一下Java的多態(tài),文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下2022-07-07