如图所示,咱们在resources文件夹中新建配置文件application.ymlmysql
结构图web
server: port: 8090 //配置端口 session-timeout: 30 tomcat.max-threads: 0 tomcat.uri-encoding: UTF-8 spring: datasource: //数据库配置 url : jdbc:mysql://localhost:3306/newbirds username : root password : mymysql driverClassName : com.mysql.jdbc.Driver
注意:key后面的冒号,后面必定要跟一个空格spring
一、在application.yml文件中咱们本身定义了age 、name 、manInfo等参数,其中manInfo引用了age、name,引用的格式"${参数名}"sql
server: //端口 port: 8081 age: 18 name: jason manInfo: "age:${age},name:${name}"
怎么使用这些配置呢?咱们建立GetManInfo文件(参照上面结构图),
使用配置格式数据库
@Value("${配置文件中的参数名}") 类型 参数名
详细以下tomcat
package com.alun; 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; /** * Created by Administrator on 2017/5/28. */ @RestController public class GetManInfo { //获取配置文件中的age @Value("${age}") private int age; //获取配置文件中的name @Value("${name}") private String name; //获取配置文件中的manInfo @Value("${manInfo}") private String manInfo; @RequestMapping(value = "/getAge",method= RequestMethod.GET) public int getAge(){ return age; } @RequestMapping(value = "/getName",method= RequestMethod.GET) public String getNme(){ return name; } @RequestMapping(value = "/getManInfo",method= RequestMethod.GET) public String getManInfo(){ return manInfo; } }
二、一个一个的@Value获取以为很烦,有办法解决么?这个....固然有啊!session
在application.yml咱们改为这样app
server: port: 8081 manInfo: age: 18 name: jason
新建一个ManInfoProperties文件,(结构参照结构图)使用
@Component
@ConfigurationProperties( prefix = "配置文件里的参数名" )测试
package com.alun; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * Created by Administrator on 2017/5/28. */ @Component @ConfigurationProperties( prefix = "manInfo" ) public class ManInfoProperties { private String age; private String name; public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
在GetManInfo里 使用 @Autowiredthis
package com.alun; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * Created by Administrator on 2017/5/28. */ @RestController public class GetManInfo { @Autowired private ManInfoProperties manInfoProperties; @RequestMapping(value = "/getManInfo",method= RequestMethod.GET) public String getManInfo(){ return manInfoProperties.getAge(); } }
多环境配置
如上图,建立application-dev.yml(测试环境)和application-prod.yml(生产)环境
application-dev.yml
server: port: 8080 manInfo: age: 18 name: jason
application-prod.yml
server: port: 8081 manInfo: age: 18 name: alun
而原有的application.yml则改为这样:
spring: profiles: active: prod
spring.profiles.active: 配置文件名(好比这里是 prod或者dev)