Lison <cundream@163.com>, v1.0.0, 2019.10.13
在 Spring Boot 中,配置文件有两种不同的格式,一个是 properties ,另一个是 yaml 。
然 properties 文件比较常见,但是相对于 properties 而言,yaml 更加简洁明了,而且使用的场景也更多,很多开源项目都是使用 yaml 进行配置(例如 Hexo)。除了简洁,yaml 还有另外一个特点,就是 yaml 中的数据是有序的,properties 中的数据是无序的,在一些需要路径匹配的配置中,顺序就显得尤为重要(例如我们在 Spring Cloud Zuul 中的配置),此时我们一般采用 yaml。
首先,当我们创建一个 Spring Boot 工程时,默认 resources 目录下就有一个 application.properties 文件,可以在 application.properties 文件中进行项目配置,但是这个文件并非唯一的配置文件,在 Spring Boot 中,一共有 4 个地方可以存放 application.properties 文件。
当前项目根目录下的config目录下当前项目根目录resource目录想的config目录下resource目录下按照顺序,四个配置文件的优先级依次降级
这四个位置是默认位置,spring boot启动,默认会从这四个位置按顺序去查找相关属性并加载。但是,这也不是绝对的,我们也可以在项目启动时自定义配置文件位置、
列如 现在在 resources 目录下创建一个 javaboy 目录,目录中存放一个 application.properties 文件,那么正常情况下,当我们启动 Spring Boot 项目时,这个配置文件是不会被自动加载的。我们可以通过 spring.config.location 属性来手动的指定配置文件位置,指定完成后,系统就会自动去指定目录下查找 application.properties 文件
此时启动项目,就会发现,项目以 classpath:/cundream/application.propertie 配置文件启动。
这是在开发工具中配置了启动位置,如果项目已经打包成 jar ,在启动命令中加入位置参数即可
java -jar properties-0.0.1-SNAPSHOT.jar --spring.config.location=classpath:/cundream/对于 application.properties 而言,它不一定非要叫 application ,但是项目默认是去加载名为 application 的配置文件,如果我们的配置文件不叫 application ,也是可以的,但是,需要明确指定配置文件的文件名。
方式和指定路径一致,只不过此时的 key 是 spring.config.name 。
首先我们在 resources 目录下创建一个 app.properties 文件,然后在 IDEA 中指定配置文件的文件名:
指定完配置文件名之后,再次启动项目,此时系统会自动去默认的四个位置下面分别查找名为 app.properties 的配置文件。当然,允许自定义文件名的配置文件不放在四个默认位置,而是放在自定义目录下,此时就需要明确指定 spring.config.location 。
配置文件位置和文件名称可以同时自定义
由于 Spring Boot 源自 Spring ,所以 Spring 中存在的属性注入,在 Spring Boot 中一样也存在。由于 Spring Boot 中,默认会自动加载 application.properties 文件,所以简单的属性注入可以直接在这个配置文件中写。
在application.properties中读取自定义属性的配置很简单,在这里不做过多的描述: 例如定义如下属性
com.github.title=Spring Boot教程--Lison然后通过**@Value(“${属性名}”)**注解来加载对应的配置属性,具体如下:
@Component public class CundreamProperties { @Value("${com.cundream.title}") private String title; @Value("${com.cundream.name}") private String name; @Value("${com.cundream.desc}") private String desc; //省略getter 和setter }注意
Book 对象本身也要交给 Spring 容器去管理,如果 Redis没有交给 Spring 容器,那么 Book 中的属性也无法从 Spring 容器中获取到值。
配置完成后,在 Controller 或者单元测试中注入 Redis对象,启动项目,就可以看到属性已经注入到对象中了。
一般来说,我们在 application.properties 文件中主要存放系统配置,这种自定义配置不建议放在该文件中,可以自定义 properties 文件来存在自定义配置。
自定义 redis.properties 文件,内容如下:
在application.properties中的各个参数之间也可以直接引用来使用,就像下面的设置:
com.cundream.name=Lison SpringBoot 系统构建 com.cundream.desc=${com.cundream.blog.name} 持续更新中com.cundream.desc参数引用了上文中定义的name和title属性
在一些情况下,有些参数我们需要希望它不是一个固定的值,比如密钥、服务端口等。Spring Boot的属性配置文件中可以通过**${random}**来产生int值、long值或者string字符串,来支持属性的随机值。
# 随机字符串 com.cundream.value=${random.value}在资源文件夹下新建一个redis.properties配置文件,内容如下:
redis.ip=127.0.0.1 redis.port=6379 redis.maxActive=1024 redis.maxIdle=200 redis.maxWait=10000 redis.testOnBorrow=true redis.testOnReturn=true redis.maxTotal=600 redis.timeBetweenEvictionRunsMillis=30000 redis.minEvictableIdleTimeMillis=30000然后新建一个实体类,来赋值配置文件的值:
@Configuration @PropertySource(value = "classpath:redis.properties") @ConfigurationProperties(prefix = "redis") public class RedisConfig { private String ip; private Integer port; private String password; private Integer maxActive; private Integer maxIdle; private Long maxWait; private Boolean testOnBorrow; private Boolean testOnReturn; private Integer expire; private Integer maxTotal; //省略getter,setter }这里,主要是引入 @ConfigurationProperties(prefix = “book”) 注解,并且配置了属性的前缀,此时会自动将 Spring 容器中对应的数据注入到对象对应的属性中,就不用通过 @Value 注解挨个注入了,减少工作量并且避免出错
新建读取类:
/** * @author : Lison * @Date: 2019/10/15 11:33 * @Description: 读取自定义配置文件 */ @Component @EnableConfigurationProperties({RedisConfig.class}) public class RedisProperties { @Autowired private RedisConfig redis; public String read(){ return redis.getIp(); } }然后再web类中添加相应的测试接口
@Autowired private RedisProperties redisProperties; @RequestMapping(value = "/read",method = RequestMethod.GET) public String read(){ return redisProperties.read(); }启动工程访问http://127.0.0.1:8082/bootbuliding/read
在现实的开发环境中,我们需要不同的配置环境;格式为application-{profile}.properties,其中{profile}对应你的环境标识,比如:
application-test.properties:测试环境application-dev.properties:开发环境application-prod.properties:生产环境修改application.properties如下:
#根据环境读取配置文件 #application-test.properties:测试环境 #application-dev.properties:开发环境 #application-prod.properties:生产环境 spring.profiles.active=dev修改application.properties如下:
新建application-dev.properties,application-test.properties, application-prod.properties,
项目GitHub地址