SpringBoot入门之分散配置
2018-03-06 20:28
369 查看
springboot默认支持两种格式的配置文件:.properties和.yml。其中.properties是属性文件,也是最常用的一种;.yml是yaml格式的文件,yaml是一种简洁的标记语言。例如:在properties文件中定义的spring.data.url,在yaml文件中的定义如下
spring: data: url:
从上可以发现yaml层次感更强,具体在项目中选择那种资源文件是没有什么规定的。
spring boot配置
简单案例
首先创建application.properties文件,并定义name=ysl,创建一个名问Name的java类
package com.ysl.entity; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class User { @Value("${name:wdd}") private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
在TestController中调用User对象
package com.ysl.controller; import com.ysl.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController public class TestController { @Autowired private User user; @RequestMapping(value = "/{name}",method = RequestMethod.GET) @ResponseBody public String home(@PathVariable("name") String name){ return "hello," + name; } }
启动该spring boot工程,在浏览器输入http://localhost:8080/ddd,显示出hello,wdd
解析
1.在springboot中默认加载
classpath:/,classpath:/config/,file:./,file:./config/路径下以
application命名的property或yaml文件;
2.参数spring.config.location设置配置文件存放位置
3.参数spring.config.name设置配置文件存放名称
配置文件获取随机数
在springboot中调用Random中的方法可以获取随机数,实例如下:在application.properties中增加age=${random.int},同事修改User对象。会为age生成一个随机值。
@Value("${age}") private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; }
使用random的时候也可以限制随机数的范围,实例如下:
${random.int(100)} : 限制生成的数字小于10 ${random.int[0,100]} : 指定范围的数字
在配置文件调用占位符
修改配置文件:
name=ysl age=${random.int} remark=hello,my name is ${name},age is ${age}
修改bean:
@Value("$remark") private String remark;
可以发现将
name修改为
userName,在配置文件中调用
${name}是工程名
去掉@Value
大家可以发现前面在bean中调用配置参数使用的为注解
@Value,在spring boot中是可以省去该注解。
配置文件:
userName=liaokailin age=${random.int[0,100]} remark=hello,my name is ${userName},age is ${age} user.address=china,hangzhou
增加
user.address=china,hangzhou,为了调用该参数来使用
@ConfigurationProperties。
@Component @ConfigurationProperties(prefix = "user") public class User { private @Value("${userName:lkl}") String name; private @Value("${age}") Integer age; private @Value("${remark}") String remark; private String address;
使用
@ConfigurationProperties需要指定
prefix,同时bean中的属性和配置参数名保持一致。
实体嵌套配置
@Component @ConfigurationProperties(prefix = "user") public class User { private @Value("${userName:lkl}") String name; private @Value("${age}") Integer age; private @Value("${remark}") String remark; private String address; private Address detailAddress;
public class Address { private String country; private String province; private String city; }
配置文件:
userName=liaokailin age=${random.int[0,100]} remark=hello,my name is ${userName},age is ${age} user.address=china,hangzhou user.detailAddress.country=china user.detailAddress.province=zhejiang user.detailAddress.city=hangzhou
这种嵌套关系如果通过yaml文件展示出来层次感会更强。
user: detailAddress: country:china province:zhejiang city:hangzhou
配置集合
一个人可能有多个联系地址,那么地址为集合
@Component @ConfigurationProperties(prefix = "user") public class User { private @Value("${userName:lkl}") String name; private @Value("${age}") Integer age; private @Value("${remark}") String remark; private String address; private Address detailAddress; private List<Address> allAddress = new ArrayList<Address>();
配置文件:
user.allAddress[0].country=china user.allAddress[0].province=zhejiang user.allAddress[0].city=hangzhou user.allAddress[1].country=china user.allAddress[1].province=anhui user.allAddress[1].city=anqing
多配置文件
spring boot设置多配置文件很简单,可以在bean上使用注解
@Profile("development")即调用
application-development.properties|yml文件,也可以调用
SpringApplication中的
etAdditionalProfiles()方法。
也可以通过启动时指定参数
spring.profiles.active。
相关文章推荐
- spring boot 入门 使用spring.profiles.active来分区配置-java编程
- SpringBoot入门-9(springboot配置thymeleaf模板)
- 【SpringBoot】SpringBoot之入门配置文件
- LiquiBase概述及(spring boot 入门配置)
- SpringBoot入门-14(springboot配置thymeleaf使用YML)
- spring boot实战(第四篇)分散配置
- 从入门到入门-Spring Boot-属性配置3
- springboot入门(一)文件配置和运行
- Spring Boot入门——tomcat配置
- spring-boot入门(七)atomikos+druid+多数据源下的分布式事务配置
- spring boot实战(第四篇)分散配置
- SpringBoot入门-12-1(springboot配置servlet,利用注解实现)
- Spring Boot 入门案例与配置说明
- Spring Boot入门——web相关配置
- Spring Boot入门1——简单例子&属性配置
- SpringBoot入门(五)——自定义配置
- SpringBoot入门-19(springboot集成mybatis注解形式增删查改properties配置,利用《script》实现动态SQL)
- Spring boot入门二 基础参数配置
- SpringBoot入门(五)——自定义配置
- Spring-boot入门之配置详解