马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
目次
一、根本语法
写一个端标语和一个路径
Controller里的方法:
然后如许写才气访问到:
这是在yml内里写的,也可以写在properties里
再访问:
二、值的写法
1.平凡范例(数字、字符串、布尔)
例子1:
设置文件:
Controller
运行访问:
例子2:
设置文件:
Controller
运行访问:
例子3:
设置文件:
controller
运行访问
2.字符串默认不消加上单引号/双引号
""例子:
设置文件
controller
运行访问
''例子:
设置文件
controller
运行访问
3.对象、Map(属性和值)(键值对)
对象范例例子(第一种方式)
设置文件
实体类Person
Controller
运行访问
对象范例例子(第二种方式)
Person实体类
再访问:
对象范例例子(行内写法)
对象范例例子(properties)
Map范例例子
设置文件
实体类PersonMap
Controller
4.数组(List、Set)
数组范例例子List1
设置文件
设置类PetsList
Controller
数组范例例子数组2
设置文件
Pets类
PetController
运行:
5.对象内里含对象
设置文件
设置类PersonDog
Dog
Controller
运行
6.@Value获取值和@ConfigurationProperties获取值比力
复杂范例封装
设置文件
PersonDog类
PersonDogController稳定
运行
SpEL
JSR303数据校验
例子@Value:
依赖
设置文件
Person类
Controller稳定
运行
现在把设置文件中的email: [email protected]改成email: 123
例子@ConfigurationProperties
设置文件
Person类
现在把设置文件中的email: [email protected]改成email: 123
复杂数据范例
7.编写设置提示
8.还支持运算
设置文件
Person类
运行:
一、根本语法
key 空格)value :表现一对键值对(空格必须有);
以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的
写一个端标语和一个路径
- server:
- port: 8081
- servlet:
- context-path: /springboot05
复制代码
Controller里的方法:
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- @Controller
- @RequestMapping("/index")
- public class IndexController {
- @ResponseBody
- @RequestMapping("/hello")
- public String hello(){
- return "hai!";
- }
- }
复制代码 然后如许写才气访问到:
这是在yml内里写的,也可以写在properties里
再访问:
原来的路径报错
得如许写才行
二、值的写法
1.平凡范例(数字、字符串、布尔)
key:value 字面直接来写
例子1:
设置文件:
Controller
- @Controller
- @RequestMapping("/test")
- public class TestController {
- @Value("${name}")
- private String name;
-
- @RequestMapping("/hello")
- @ResponseBody
- public String sayHello(){
- System.out.println(name);
- return "say Hello";
- }
- }
复制代码 运行访问:
例子2:
设置文件:
Controller
- @Controller
- @RequestMapping("/test")
- public class TestController {
- @Value("${name}")
- private String name;
- @Value("${age}")
- private String age;
- @RequestMapping("/hello")
- @ResponseBody
- public String sayHello(){
- System.out.println(name);
- System.out.println(age);
- return "say Hello";
- }
- }
复制代码 运行访问:
例子3:
设置文件:
controller
- @Controller
- @RequestMapping("/test")
- public class TestController {
- @Value("${name}")
- private String name;
- @Value("${age}")
- private String age;
- @Value("${boss}")
- private String boss;
- @RequestMapping("/hello")
- @ResponseBody
- public String sayHello(){
- System.out.println(name);
- System.out.println(age);
- System.out.println(boss);
- return "say Hello";
- }
- }
复制代码 运行访问
2.字符串默认不消加上单引号/双引号
"":双引号;不会转义字符串内里的特别字符;特别字符会作为本身想表现的意思
name: "zhangsan \n lisi":输出;zhangsan 换行 lisi
""例子:
设置文件
controller
Controller跟 1.平凡的值例子1 内里的一样,没有变
运行访问
'':单引号;会转义特别字符,特别字符终极只是一个平凡的字符串数据
name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi
''例子:
设置文件
controller
Controller跟 1.平凡的值例子1 内里的一样,没有变
运行访问
3.对象、Map(属性和值)(键值对)
key: value :在下一行来写对象的属性和值的关系;留意缩进
对象照旧k: v的方式
行内写法:
对象范例例子(第一种方式)
设置文件
实体类Person
- package com.qcby.springboot05.controller;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- //@Value是spring的东西,new肯定不行,所以得把它交给spring去管理,所以加个@Component注解
- @Component
- public class Person {
- @Value("${person.name}")
- private String name;
- @Value("${person.age}")
- private Integer age;
- @Value("${person.boss}")
- private Boolean boss;
- public Person() {
- }
- public Person(String name, Integer age, Boolean boss) {
- this.name = name;
- this.age = age;
- this.boss = boss;
- }
- 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;
- }
- public Boolean getBoss() {
- return boss;
- }
- public void setBoss(Boolean boss) {
- this.boss = boss;
- }
- @Override
- public String toString() {
- return "Person{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", boss=" + boss +
- '}';
- }
- }
复制代码 Controller
- @Controller
- @RequestMapping("/test")
- public class TestController {
-
- @Autowired
- private Person person;
- @RequestMapping("/hello")
- @ResponseBody
- public String sayHello(){
- System.out.println(person);
- return "say Hello";
- }
- }
复制代码 运行访问
对象范例例子(第二种方式)
Person实体类
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
- //第一种方式:
- // @Value是spring的东西,new肯定不行,所以得把它交给spring去管理,所以加个@Component注解
- @Component
- //第二种方式
- // 加一个前缀,是springBoot的注解ConfigurationProperties;括号里面不加东西,默认是value
- //写上的话就是prefix (prefix="person")
- //下面的@Value不用写了
- @ConfigurationProperties("person")
- public class Person {
- //@Value("${person.name}")
- private String name;
- //@Value("${person.age}")
- private Integer age;
- //@Value("${person.boss}")
- private Boolean boss;
- public Person() {
- }
- public Person(String name, Integer age, Boolean boss) {
- this.name = name;
- this.age = age;
- this.boss = boss;
- }
- 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;
- }
- public Boolean getBoss() {
- return boss;
- }
- public void setBoss(Boolean boss) {
- this.boss = boss;
- }
- @Override
- public String toString() {
- return "Person{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", boss=" + boss +
- '}';
- }
- }
复制代码 再访问:
对象范例例子(行内写法)
如许也是能访问到的
对象范例例子(properties)
写到application.properties内里也是一样的
运行
第一次控制台输出的时间 张三 是 ??,编码标题,办理:
Map范例例子
用的@ConfigurationProperties的方式
设置文件
实体类PersonMap
- @Component
- @ConfigurationProperties(prefix = "my")
- public class PersonMap {
- private Map<String, String> map;
- public Map<String, String> getMap() {
- return map;
- }
- public void setMap(Map<String, String> map) {
- this.map = map;
- }
- @Override
- public String toString() {
- return "PersonMap{" +
- "map=" + map +
- '}';
- }
- }
复制代码 Controller
- @Controller
- @RequestMapping("/api")
- public class PersonMapController {
- @Autowired
- private PersonMap personMap;
- @RequestMapping("/map")
- @ResponseBody
- public String getMap() {
- System.out.println(personMap);
- return "map";
- }
- }
复制代码 运行访问:
4.数组(List、Set)
用- 值表现数组中的一个元素
数组范例例子List1
设置文件
设置类PetsList
- @Component
- @ConfigurationProperties(prefix = "my1")
- public class PetsList {
- private List<String> pets;
- public PetsList() {
- }
- public PetsList(List<String> pets) {
- this.pets = pets;
- }
- public List<String> getPets() {
- return pets;
- }
- public void setPets(List<String> pets) {
- this.pets = pets;
- }
- @Override
- public String toString() {
- return "PetsList{" +
- "pets=" + pets +
- '}';
- }
- }
复制代码 Controller
- @Controller
- @RequestMapping("/pets")
- public class PetsListController {
- @Autowired
- private PetsList petsList;
- @RequestMapping("/list")
- @ResponseBody
- public String petsList(){
- System.out.println(petsList);
- return "list";
- }
- }
复制代码 运行访问:
数组范例例子数组2
设置文件
设置文件这里用{}不可,输出为null,必须用[]
Pets类
- @Component
- @ConfigurationProperties(prefix = "my2")
- public class Pets {
- private String[] pets;
- public String[] getPets() {
- return pets;
- }
- public void setPets(String[] pets) {
- this.pets = pets;
- }
- @Override
- public String toString() {
- return "Pets{" +
- "pets=" + Arrays.toString(pets) +
- '}';
- }
- }
复制代码 PetController
- @Controller
- @RequestMapping("/pet")
- public class PetController {
- @Autowired
- private Pets pets;
- @ResponseBody
- @RequestMapping("/pets")
- public String pets(){
- System.out.println(pets);
- return "数组";
- }
- }
复制代码 运行:
5.对象内里含对象
设置文件
设置类PersonDog
- @Component
- @ConfigurationProperties(prefix = "person1")
- public class PersonDog {
- private String name;
- private int age;
- private Dog dog;
- public PersonDog() {
- }
- public PersonDog(String name, int age, Dog dog) {
- this.name = name;
- this.age = age;
- this.dog = dog;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public Dog getDog() {
- return dog;
- }
- public void setDog(Dog dog) {
- this.dog = dog;
- }
- @Override
- public String toString() {
- return "PersonDog{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", dog=" + dog +
- '}';
- }
- }
复制代码 Dog
- public class Dog {
- private String name;
- private int age;
- public Dog() {
- }
- public Dog(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "Dog{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
复制代码 Controller
- @Controller
- @RequestMapping("/person")
- public class PersonDogController {
- @Autowired
- private PersonDog personDog;
- @RequestMapping("/dog")
- @ResponseBody
- public String sayHello(){
- System.out.println(personDog);
- return "对象里面含对象";
- }
- }
复制代码 运行
6.@Value获取值和@ConfigurationProperties获取值比力
| @ConfigurationProperties
| @Value
| 功能
| 批量注入设置文件中的属性
| 一个个指定
| 疏松绑定(疏松语法)
| 支持
| 不支持
| SpEL
| 不支持
| 支持
| JSR303数据校验
| 支持
| 不支持
| 复杂范例封装
| 支持
| 不支持
| 复杂范例封装
支持的写过了,就是上面的对象内里含对象、数组那的也算、Map什么的,现在写不支持的
设置文件
PersonDog类
- @Component
- public class PersonDog {
- @Value("${person1.name}")
- private String name;
- @Value("${person1.age}")
- private int age;
- @Value("${person1.dog}")
- private Dog dog;
- public PersonDog() {
- }
- public PersonDog(String name, int age, Dog dog) {
- this.name = name;
- this.age = age;
- this.dog = dog;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public Dog getDog() {
- return dog;
- }
- public void setDog(Dog dog) {
- this.dog = dog;
- }
- @Override
- public String toString() {
- return "PersonDog{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", dog=" + dog +
- '}';
- }
- }
复制代码 PersonDogController稳定
- @Controller
- @RequestMapping("/person")
- public class PersonDogController {
- @Autowired
- private PersonDog personDog;
- @RequestMapping("/dog")
- @ResponseBody
- public String sayHello(){
- System.out.println(personDog);
- return "复杂类型封装";
- }
- }
复制代码 运行
报错了,可见不支持
SpEL
@Value内里支持${person.name}这种写法
JSR303数据校验
比如有一个属性email(设置文件里也有哈),如果使用的是@Value,它是不支持@Email举行表单数据校验的
例子@Value:
依赖
要写数据校验,须要引入依赖
- <!-- 导入字段校验:hiberrate validator -->
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-validator</artifactId>
- <version>5.3.6.Final</version>
- </dependency>
复制代码 设置文件
Person类
Controller稳定
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- @Controller
- @RequestMapping("/test")
- public class TestController {
- @Autowired
- private Person person;
- @RequestMapping("/hello")
- @ResponseBody
- public String sayHello(){
- System.out.println(person);
- return "say Hello";
- }
- }
复制代码 运行
现在把设置文件中的email: [email protected]改成email: 123
没有报错,直接输出了,可见@Value是不支持数据校验的
例子@ConfigurationProperties
设置文件
Person类
须要加校验注解
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
- import org.springframework.validation.annotation.Validated;
- import javax.validation.constraints.Email;
- @Component
- @ConfigurationProperties("person")
- @Validated //校验注解
- public class Person {
- private String name;
- private Integer age;
- private Boolean boss;
- @Email
- private String email;
- public Person() {
- }
- public Person(String name, Integer age, Boolean boss, String email) {
- this.name = name;
- this.age = age;
- this.boss = boss;
- this.email = email;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- 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;
- }
- public Boolean getBoss() {
- return boss;
- }
- public void setBoss(Boolean boss) {
- this.boss = boss;
- }
- @Override
- public String toString() {
- return "Person{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", boss=" + boss +
- ", email=" + email +
- '}';
- }
- }
复制代码 controller跟例子1一样,启动运行


现在把设置文件中的email: [email protected]改成email: 123
再启动:
复杂数据范例
比如数组、map等这些,@ Value都是不支持的
Map:
数组
7.编写设置提示
加一个依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
- </dependency>
复制代码 然后crtl+f9,把原来设置文件中的东西表明掉,现在重新写就有提示了:(就是得先有谁人实体类)
8.还支持运算
设置文件
Person类
运行:
|