Spring boot 总结之配置文件处理


声明:本文转载自https://my.oschina.net/huaxian8812/blog/1618095,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

背景

在项目开发过程中,很多东西我们都需要做成可配置化,那么使用spring的时候我们都有哪些使用配置文件的方式呢?接下来我们就来讲一下。

定义配置文件

我们以最长使用的jdbc为例

jdbc.url=jdbc:mysql://localhost/test jdbc.username=dbuser jdbc.password=dbpass jdbc.driver-class-name=com.mysql.jdbc.Driver 

不使用spring boot之前我们怎么处理

XML方式配置

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">     <property name="locations" value="classpath:jdbc.properties"/> </bean>  <bean id="dataSource" destroy-method="close"         class="org.apache.commons.dbcp.BasicDataSource">     <property name="driverClassName" value="${jdbc.driverClassName}"/>     <property name="url" value="${jdbc.url}"/>     <property name="username" value="${jdbc.username}"/>     <property name="password" value="${jdbc.password}"/> </bean> 

Java Bean Configuration

使用Environment

@Component @PropertySource("classpath:jdbc.properties") public class ConfigurationWithEnviroment {      @Autowired     private Environment env;      @PostConstruct     public void init() {         System.out.println("jdbc.url: " + env.getProperty("jdbc.url"));         // 控制台输出jdbc.url: jdbc:mysql://localhost/test     }  } 

记得在使用Environment引入配置文件配置Mybatis的时候,报出异常,Environment注入为空的异常,原因是BeanFactoryPostProcessor Bean创建得比较早。详细请看: https://stackoverflow.com/questions/19421092/autowired-environment-is-null https://jira.spring.io/browse/SPR-13683

使用EnvironmentAware

@Component @PropertySource("classpath:jdbc.properties") public class ConfigurationWithEnviroment implements EnvironmentAware{          private Environment env;      @Override     public void setEnvironment(Environment env) {         this.env = env;     }          @PostConstruct     public void init() {         System.out.println("jdbc.url: " + env.getProperty("jdbc.url"));         //控制台输出jdbc.url: jdbc:mysql://localhost/test     }  } 

使用@Value注解

使用@Value注解我们需要配置占位符org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">     <property name="locations" value="classpath:com/foo/jdbc.properties"/> </bean> 

或使用Java Bean配置

public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {     return new PropertySourcesPlaceholderConfigurer(); } 

Spring boot中默认会配置该选项,详细代码PropertyPlaceholderAutoConfiguration类

Spring Boot里的标准配置文件application.[properties|yaml]

Spring Boot官方统一配置文件,可以配置所有的Spring Boot中的配置,从datasource到web到安全等等。官方支持properties和yaml两种文件格式。

配置文件里的替换符

Spring Boot的配置文件支持上下文依赖替换,怎么说呢,就是我们可以使用前面定义的属性来达到公用的目的。 例如:

server:   port: ${random.int[8080,8088]}   name: ${random.name}  placeholder: ${server.name} //使用前面定义的server.name 

配置文件里的随机数

server:   port: ${random.int[8080,8088]} //8080到8088之间的随机数   name: ${random.name} //随机字符串   lessTen: ${random.int(10)}// 小于10   uuid: ${random.uuid} // uuid  @Autowired private Environment env;  @PostConstruct public void init() {     System.out.println(env.getProperty("server.port"));     System.out.println(env.getProperty("server.name"));     System.out.println(env.getProperty("server.lessTen"));     System.out.println(env.getProperty("server.uuid")); } 

输出

8085 adb4648465e5675aa27ba28d47f88015 7 f5286b15-b37c-4e0a-aa42-baef2c663e20 

Spring Boot里的注解@ConfigurationProperties

my:    list:      - name: java     - name: php     - name: go 
@ConfigurationProperties("my") @Component public class Configuration {      private final List<String> list = new ArrayList<String>(); //对应配置文件里的my list name.      public List<String> getList() {         return list;     } } 

本文发表于2018年02月04日 10:31
(c)注:本文转载自https://my.oschina.net/huaxian8812/blog/1618095,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 1724 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1