一、概述
application.properties就是springboot的属性配置文件
在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配置足以满足正常的功能开发。
除此之外,还有application.yml形式的配置文件;对比如下;
server.port=8080 server.session-timeout=30 server.context-path= server.tomcat.max-threads=0 server.tomcat.uri-encoding=UTF-8 spring.datasource.url = jdbc:mysql://localhost:3306/spring spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.jdbc.Driver # Specify the DBMS spring.jpa.database = MYSQL # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update) spring.jpa.hibernate.ddl-auto = update # Naming strategy spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # stripped before adding them to the entity manager) spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
server: port: 8080 session-timeout: 30 tomcat.max-threads: 0 tomcat.uri-encoding: UTF-8 spring: datasource: url : jdbc:mysql://localhost:3306/springboot username : root password : root driverClassName : com.mysql.jdbc.Driver jpa: database : MYSQL show-sql : true hibernate: ddl-auto : update naming-strategy : org.hibernate.cfg.ImprovedNamingStrategy properties: hibernate:
默认生成的为application.properties
# 补充:springboot日志详解
二、修改默认配置
spring-boot自带了很多默认配置,所有默认配置可以参考官方文档:点击查看所有默认配置
示例:修改端口和context-path:
server.port=8081
server.context-path=/demo
// 新版的IDEA写此配置文件也会给出对应默认配置名称的提示,赞!
当然,通过jar方式运行也可以使用--进行参数设置,这是专门用于对属性文件进行配置的:
java -jar xxx.jar --server.port=8888
当然,properties写起来比较费事,我们推荐的是yml(压ml)形式的配置:
这里必须提醒,yml配置文件分割处必须有空格:当然IDEA也会帮我们检查yml语法,正确的配置是会高亮的,yml文件本身也会有特殊图标显示的
这里我们就把默认的properties删除了,使用更加简洁的yml配置:
server:
port: 8082
context-path: /demo
如果已经是properties,可以通过在线工具转换:http://www.toyaml.com/
效果:
三、自定义属性配置
1.在yml文件中定义:
salary: 10010
name: jiangbei
2.使用@Value注解读取值:
使用@Value的类如果被其他类作为对象引用,必须要使用注入的方式,而不能new
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* 测试demo的controller
*
* @author zcc ON 2018/2/8
**/
@RestController
public class HelloController {
@Value("${name}")
private String name;
@Value("${salary}")
private Integer salary;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
return "name:" + name + " salary:" + salary;
}
}
页面效果:
// 可以看到,配置文件到属性的过程,会自动进行类型转换
读取多级属性:
name:
first_name: jiangbei
last_name: z
@Value("${name.first_name}")
private String fname;
@Value("${name.last_name}")
private String lname;
属性中引用其他属性:类似shell变量
name:
first_name: jiangbei
last_name: z
full_name: "full name: ${name.first_name} ${name.last_name}"
使用bean进行属性映射
需要特别注意的是bean上面的两个注解:@Componet和@ConfigurationProperties,通过prefix来匹配前缀(类似分组思想),当然也可以
通过 @ConfigurationProperties(prefix = "master.ds",locations = "classpath:application.properties") 的形式,指定配置文件
person:
name: jiangbei
age: 18
sex: M
package com.example.demo.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 映射属性配置的bean
*
* @author zcc ON 2018/2/8
**/
@ConfigurationProperties(prefix = "person")
@Component
public class PersonProp {
private String name;
private Integer age;
private String sex;
// getter setter未列出
}
@RestController
public class HelloController {
@Autowired
private PersonProp person;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
return person.getName();
}
}
多配置文件切换:
例如生产和开发的配置文件需要切换,那我们只要定义application-xxx.yml,然后在application.yml里面使用spring.profile.active切换即可!
在application.yml中进行配置:
spring:
profiles:
active: dev
这个时候再在application.yml文件里面写配置的话,dev里面也是可以直接用到的!
更多属性特征与使用,参考: http://blog.didispace.com/springbootproperties/