spring boot 项目启动配置

数据库的配置:

spring.datasource.url: jdbc:mysql://127.0.0.1:3306/
spring.datasource.username: root
spring.datasource.password: 123456
spring.datasource.driver-class-name: com.mysql.jdbc

 

用.yml文件代替.properties文件

server:
   port: 8082
   context-path: /girl
girl:
cupSize: B
age: 18
// content: "cupSize: ${cupSize}, age: ${age}"
@value("${cupSize}")             //在yml配置中有  cupSize:B  实现配置内容的注入
privat String cupSize;
@value("${age}")             //在yml配置中有  cupSize:B
privat Integer age;
@value("${content}")             //在yml配置中有  cupSize:B
privat String content;

如果要用到多个属性,新建一个类

@component               //别的类用@autowird注解他的时候加上这个注解
@ConfigurationProperties(prefix = "girl") //获取前缀是girl的属性 public class GirlProperties { private String cupSize; private Integer age; +get set 方法 }

如果有不同配置,改起来麻烦时,

建立application-dev.yml  和 application-prod.yml  ,

application-dev写

server:
   port: 8080
girl:
    cupSize: F
    age: 18

application-prod写

server:
   port: 8081
girl:
    cupSize: B
    age: 18

 

然后在application.yml中清空配置后,写

spring:
   profiles:
      active: dev

 

posted @ 2018-10-11 13:49  Evangenia  阅读(265)  评论(0编辑  收藏  举报