1:在yml文件中设置三个属性为name,age,heigth[实际上分别为String,int,double类型]
2:通过@Value注解获取配置属性值,在这里指定数据类型.
[如果配置文件中没有配置属性,而在属性上使用了@Value注解,那么服务是无法启动成功的]
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;
@RestController
public class HelloSpringBoot {
@Value("${name}")
private String name;
@Value("${age}")
private Integer age;
@Value("${height}")
private Double height;
@RequestMapping(value = "/getUser", method = RequestMethod.GET)
public String say() {
return "姓名:"+this.name+";年龄:"+age+";身高:"+height;
}
}
3:访问:
4:配置文件中属性嵌套
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;
@RestController
public class HelloSpringBoot {
@Value("${Content}")
private String Content;
@RequestMapping(value = "/getUser", method = RequestMethod.GET)
public String say() {
return "所有内容:"+Content;
}
}
5:对类使用注解传值
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="person")
public class Person {
private String name;
private Integer age;
public Person() {
super();
}
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
配置:
server:
port: 8080
servlet:
context-path: /springboot
person:
name: 小明
age: 15
height: 170.05
控制层:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.springboot.entity.Person;
@RestController
public class PersonSpringBoot {
@Autowired
private Person person;
@RequestMapping(value="/getUserInfo",method=RequestMethod.GET)
public Integer getUserInfo() {
return person.getAge();
}
}
访问:
6:多个配置文件
场景:开发环境和sit测试环境的值不同,就需要配置不同的文件,而不是修改同一个配置文件
在默认的application.yml中引入其他配置,这里引入的是生产环境的配置
访问:
8:访问时传参数
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/springboot")
public class PersonSpringBoot {
//http://127.0.0.1:8088/springboot/getInfo1/55
@RequestMapping(value="/getInfo1/{id}",method=RequestMethod.GET)
public String getInfo1(@PathVariable("id") Integer requestId) {
return "1:请求参数Id的值为:"+requestId;
}
//http://127.0.0.1:8088/springboot/22/getInfo2
@GetMapping(value="/{id}/getInfo2")
public String getInfo2(@PathVariable("id") Integer requestId) {
return "2:请求参数Id的值为:"+requestId;
}
//http://127.0.0.1:8088/springboot/22/getInfo3
@PostMapping(value="/{id}/getInfo3")
public String getInfo3(@PathVariable("id") Integer requestId) {
return "3:请求参数Id的值为:"+requestId;
}
//http://127.0.0.1:8088/springboot/getInfo4?id=44
@PostMapping(value="/getInfo4")
public String getInfo4(@RequestParam("id") Integer requestId) {
return "4:请求参数Id的值为:"+requestId;
}
}
访问