目录
6.@Value获取值和@ConfigurationProperties获取值比较
[现在把配置文件中的email: 123@qq.com改成email: 123](#现在把配置文件中的email: 123@qq.com改成email: 123)
[现在把配置文件中的email: 123@qq.com改成email: 123](#现在把配置文件中的email: 123@qq.com改成email: 123)
一、基本语法
key:(空格)value :表示一对键值对(空格必须有);
以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的
写一个端口号和一个路径
bash
server:
port: 8081
servlet:
context-path: /springboot05
Controller里的方法:
java
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
java
@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
java
@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
java
@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
java
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
java
@Controller
@RequestMapping("/test")
public class TestController {
@Autowired
private Person person;
@RequestMapping("/hello")
@ResponseBody
public String sayHello(){
System.out.println(person);
return "say Hello";
}
}
运行访问
对象类型例子(第二种方式)
Person实体类
java
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
java
@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
java
@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
java
@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
java
@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类
java
@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
java
@Controller
@RequestMapping("/pet")
public class PetController {
@Autowired
private Pets pets;
@ResponseBody
@RequestMapping("/pets")
public String pets(){
System.out.println(pets);
return "数组";
}
}
运行:
5.对象里面含对象
配置文件
配置类PersonDog
java
@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
java
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
java
@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类
java
@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不变
java
@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:
依赖
要写数据校验,需要引入依赖
XML
<!-- 导入字段校验:hiberrate validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.3.6.Final</version>
</dependency>
配置文件
Person类
Controller不变
java
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: 123@qq.com改成email: 123
没有报错,直接输出了,可见@Value是不支持数据校验的
例子 @ConfigurationProperties
配置文件
Person类
需要加校验注解
java
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: 123@qq.com改成email: 123
再启动:
复杂数据类型
比如数组、map等这些,@ Value都是不支持的
Map:
数组
7.编写配置提示
加一个依赖
XML
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
然后crtl+f9,把原来配置文件中的东西注释掉,现在重新写就有提示了:(就是得先有那个实体类)