# girl
**Repository Path**: tinygg/girl
## Basic Information
- **Project Name**: girl
- **Description**: spring boot 学习笔记
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2018-03-14
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
1.启动spring boot的三种方式
A.IDEA 自带的启动
B.mvn spring-boot:run
C.mvn install 安装生成jar包
cd target
java -jar demo-0.0.1-SNAPSHOT.jar
2.属性文件=>yml
A.更方便(同一个东西的前部分可以缩略)
application.properties ->application.yml
server.port=8081
server.context-path=/girl
=>
server:
port: 8081
context-path: /girl
B.可以通过Value注解获取
server:
port: 8081
context-path: /girl
age: 18
sex: girl
########################################
...
@RestController
public class HelloController {
@Value("${sex}")
private String sex;
@Value("${age}")
private Integer age;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String say(){
return "Hello Spring boot...";
}
}
########################################
C.注解可以引用注解
server:
port: 8081
context-path: /girl
age: 18
sex: girl
content: "this's a ${sex} and age is ${age}."
########################################
@RestController
public class HelloController {
@Value("${sex}")
private String sex;
@Value("${age}")
private Integer age;
@Value("${content}")
private String content;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String say() {
return "Hello Spring boot..." + age + sex + content;
}
}
########################################
D.通过注解可以将配置文件映射成一个对象
########################################
server:
port: 8081
context-path: /girl
age: 18
sex: girl-dev
content: "this's a ${sex} and age is ${age}."
girl:
age: 18
sex: girl
content: "===this's a ${sex} and age is ${age}."
###
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
private String sex;
private Integer age;
private String content;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
###
@RestController
public class HelloController {
@Value("${sex}")
private String sex;
@Value("${age}")
private Integer age;
@Value("${content}")
private String content;
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String say() {
return "Hello Spring boot..." + age + sex + content+ girlProperties.getContent();
}
}
########################################
E.注解可以通过运行参数修改
java -jar demo-0.0.1-SNAPSHOT.jar --age=19
http://127.0.0.1:8081/girl/hello
返回
Hello Spring boot...19girlthis's a girl and age is 19.
3.通过配置文件的特性可以通过运行参数切换运行环境
A.复制application.yml为application-dev.yml和application-prod.yml
同时将application.yml修改为
spring:
profiles:
active: dev或prod
即可达到切换运行配置文件的目的
B.通过java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev或prod
达到通过传参实现切换配置文件的目的
4.Controller注解
【注意:RestController注解等同于Controller+ResponseBody】
Mapping需返回模板路径
A.pom.xml新增依赖
Name: Sebastian.
Surname: Pepper.
Nationality: Saturn.
Name: Sebastian.
Surname: Pepper.
Nationality: Saturn.
Name: Sebastian.
Surname: Pepper.
Nationality: Saturn.