3、项目属性配置
- 在application.properties当中配置端口号
- server.port=8081
- 在application.properties当中配置项目路径
- server.context-path=/girl
- 启动项目
- 访问 localhost:8081/girl/hello
- 也可以将 pplication.properties也可以写成
- application.yml文件
通过注解,将application.yml当中的变量在.java文件当中取出使用- @Value("${cupSize}")
- 在yml文件当中添加cupSize:xxx 属性
- 在配置当中使用当前配置
- 在.yml文件当中使用封装
- 然后创建针对封装的类使用配置文件当中的数据
- 然后在主文件当中调用
- 针对两套不同的配置文件,开发环境和生产环境
- application-dev.yml
- application-prod-yml
- 需要在application.yml文档当中配置使用哪一个配置文件
- 生产环境和开发环境同时使用
- 使用两套系统,一套打包成jar包执行,一套可以在IDE当中执行,分别设置不同的端口就可以了,然后在命令式使用jar执行的时候,使用如下的命令
- java -jar girl-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
- 完
cupSize: B
age: 18
content: "cupSize: ${cupSize},age:${age}"
3
1
cupSize: B
2
age: 18
3
content: "cupSize: ${cupSize},age:${age}"
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
spring:
profiles:
active: dev
1
1
spring:
2
profiles:
3
active: dev