3、项目属性配置

  1. 在application.properties当中配置端口号
    1. server.port=8081
  2. 在application.properties当中配置项目路径
    1. server.context-path=/girl
  3. 启动项目
    1. 访问 localhost:8081/girl/hello
  4. 也可以将 pplication.properties也可以写成
    1. application.yml文件



  1. 通过注解,将application.yml当中的变量在.java文件当中取出使用
    1. @Value("${cupSize}")
    2. 在yml文件当中添加cupSize:xxx 属性
  2. 在配置当中使用当前配置
cupSize: B
age: 18
content: "cupSize: ${cupSize},age:${age}"
3
 
1
cupSize: B
2
age: 18
3
content: "cupSize: ${cupSize},age:${age}"
在.yml文件当中使用封装
server:
  port: 8080
girl:
  cupSize: B
  age: 18
6
 
1
server:
2
  port: 8080
3
girl:
4
  cupSize: B
5
  age: 18
6
 
            
然后创建针对封装的类使用配置文件当中的数据
package com.girl;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Created by sunnyangzs on 2018/3/26.
 */
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {

    private  String cupSize;
    private Integer age;

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
1
32
 
1
package com.girl;
2
 
            
3
import org.springframework.boot.context.properties.ConfigurationProperties;
4
import org.springframework.stereotype.Component;
5
 
            
6
/**
7
 * Created by sunnyangzs on 2018/3/26.
8
 */
9
@Component
10
@ConfigurationProperties(prefix = "girl")
11
public class GirlProperties {
12
 
            
13
    private  String cupSize;
14
    private Integer age;
15
 
            
16
    public String getCupSize() {
17
        return cupSize;
18
    }
19
 
            
20
    public void setCupSize(String cupSize) {
21
        this.cupSize = cupSize;
22
    }
23
 
            
24
    public Integer getAge() {
25
        return age;
26
    }
27
 
            
28
    public void setAge(Integer age) {
29
        this.age = age;
30
    }
31
}
32
 
            
然后在主文件当中调用
package com.girl;

import org.springframework.beans.factory.annotation.Autowired;
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 sunnyangzs on 2018/3/25.
 */
@RestController
public class HelloController {


    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String say(){
        return girlProperties.getCupSize();
    }

}
x
 
1
package com.girl;
2
 
            
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.beans.factory.annotation.Value;
5
import org.springframework.web.bind.annotation.RequestMapping;
6
import org.springframework.web.bind.annotation.RequestMethod;
7
import org.springframework.web.bind.annotation.RestController;
8
 
            
9
/**
10
 * Created by sunnyangzs on 2018/3/25.
11
 */
12
@RestController
13
public class HelloController {
14
 
            
15
 
            
16
    @Autowired
17
    private GirlProperties girlProperties;
18
 
            
19
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
20
    public String say(){
21
        return girlProperties.getCupSize();
22
    }
23
 
            
24
}
25
 
            
针对两套不同的配置文件,开发环境和生产环境
  1. application-dev.yml
  2. application-prod-yml
  3. 需要在application.yml文档当中配置使用哪一个配置文件
spring:
  profiles:
    active: dev
1
 
1
spring:
2
  profiles:
3
    active: dev
生产环境和开发环境同时使用
  1. 使用两套系统,一套打包成jar包执行,一套可以在IDE当中执行,分别设置不同的端口就可以了,然后在命令式使用jar执行的时候,使用如下的命令
    1. java -jar girl-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod




转载于:https://www.cnblogs.com/sunnyangzs/p/8648463.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值