application.properties
server.port=8080
server.context-path=/girl
这个girl相当于项目名,访问时:localhost:8080/girl/xxxx
application.yml
这种方式更好:
server:
port: 8080
context-path: /girl
也可以声明自定义配置:
height: 180
title: jimo
那么怎么使用呢?--使用注入
@Value("${title}")
private String title;
@Value("${height}")
private Integer height;
在配置里嵌套使用:
content: "content is ${height},${title}"
但是问题来了,这样写配置多了,或者说很多地方要用的话就很麻烦,每个地方都要写那么可以这样。
配置升级
my.height: 180
my.title: jimo
注入到一个类:
@Component
@ConfigurationProperties(prefix = "my")
public class MenProperty {
private String title;
private Integer height;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
}
然后可以直接访问这个类:
@Autowired
MenProperty mp;
@RequestMapping("/hello")
public String hello() {
return mp.getTitle() + mp.getHeight();
}
不同环境下的配置
有时候分开发环境和生产环境,这就需要两份配置文件:
application-dev.yml和
application-prod.yml
在application.yml里配置:
spring:
profiles:
active:
- dev