SpringBoot系列<三>内置配置及自定义配置

来源:互联网 发布:网络广告宣传图片 编辑:程序博客网 时间:2024/04/17 03:02

在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配置足以满足正常的功能开发。

如果需要修改自定义修改默认配置,spring boot 提供了很简便的方法,只需要在application.properties 中添加修改相应的配置。(spring boot启动的时候会读取application.properties这份默认配置)

一、修改默认配置
例1、spring boot 开发web应用的时候,默认tomcat的启动端口为8080,如果需要修改默认的端口,则需要在application.properties 添加以下记录:

server.port=80

重启项目,启动日志可以看到:Tomcat started on port(s): 80(http) 启动端口为8888,浏览器中访问 http://localhost:80能正常访问。

更多:application.properties 全部配置项参考:Spring Boot属性文件配置文档(全部)

二、自定义属性配置

注:我这里使用的的是application.yml方式配置的,具体SpringBoot支持的配置及配置优先级后面会讲述;

在application.yml中除了可以修改默认配置,我们还可以在这配置自定义的属性,并在实体bean中加载出来。

application.yml中配置自定义属性如下:

com:  mySelf:    profile:     name:     age:    component: 当前分支是${com.mySelf.profile},描述:${com.mySelf.name},自定义属性age:${com.mySelf.age}

第一种:使用spring支持的@Value()加载

@Component@Datapublic class MySelf {    @Value("${com.mySelf.profile}")    private String profile;    @Value("${com.mySelf.name}")    private String name;    @Value("${com.mySelf.age}")    private String age;}

第二种:使用@ConfigurationProperties(prefix=”“) 设置前缀,属性上不需要添加注解。

@Data@Component@ConfigurationProperties(prefix = "com.mySelf")public class MySelfTwo {    private String profile;    private String name;    private String age;}

注:@Data是lombok注解

在controller中注入并使用这两个Bean:

@Value("${com.mySelf.profile}")    private String myName;    @Autowired    private MySelf mySelf;    @Autowired    private MySelfTwo mySelfTwo;    @ResponseBody    @RequestMapping(value = "myConfig")    public String getMyConfig(){        String result=null;        //result=myName;        result=mySelf.toString();        //result=mySelfTwo.toString();        return result;    }

三、application.properties 属性配置详解

1、参数引用与random随机数方法的使用
在application.properties内可以直接通过${}引用其他属性的值,如下:

com:  mySelf:    profile:     name:     age:    component: 当前分支是${com.mySelf.profile},描述:${com.mySelf.name},自定义属性age:${com.mySelf.age}

在application.properties中如果需要获取随机数,可以通过${random},如下:

#获取随机字符串com.sam.randomValue=${random.value}#获取随机字符串:${random.value}#获取随机int:${random.int}#获取10以内的随机数:${random.int(10)}#获取10-20的随机数:${random.int[10,20]}#获取随机long:${random.long}#获取随机uuid:${random.uuid}

2、多环境配置

yml格式的配置文件方式如下:

---spring:  profiles: dev---spring:  profiles: prod

以—分隔环境,各环境公共部分配置可以抽取放在最上面

用命令运行jar包启动应用的时候,可以指定相应的配置:

--spring.profiles.active=dev

附:配置方式和优先级

这些方式优先级如下:a. 命令行参数b. 来自java:comp/env的JNDI属性c. Java系统属性(System.getProperties())d. 操作系统环境变量e. RandomValuePropertySource配置的random.*属性值f. jar外部的application-{profile}.properties或application.yml(带spring.profile)配置文件g. jar内部的application-{profile}.properties或application.yml(带spring.profile)配置文件h. jar外部的application.properties或application.yml(不带spring.profile)配置文件i. jar内部的application.properties或application.yml(不带spring.profile)配置文件j. @Configuration注解类上的@PropertySourcek. 通过SpringApplication.setDefaultProperties指定的默认属性

注:命令行参数这种jar包指定参数启动应用的方式,可能是不安全的,我们可以设置禁止这种方式启动应用,如下:

springApplication.setAddCommandLineProperties(false);