目录
1.方法一:spring注解注入
1.1 创建Student.java
package com.example.demo.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//注入配置方法一:Spring注解方法
@Component //相当于配置文件中 <bean id="user" class="当前注解的类"/>
public class Student {
@Value("zhang")
private String name;
@Value("21")
private Integer 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;
}
public Student() {
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
1.2 在test测试中进行测试
package com.example.demo;
import com.example.demo.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Autowired//@Autowired是按类型自动装配的,同一类型的对象需唯一
private Student student;
@Test
void contextLoads() {
System.out.println(student);
}
}
2.方法二:使用yaml注入配置
2.1更改配置文件
把application.properties改成application.yaml
person:
name: zhang
age: 3
happy: false
birth: 2000/01/01
maps: {k1: v1,k2: v2}
lists:
- code
- girl
- music
2.2 创建Person.java
package com.example.demo.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
//注入配置方法二:Yaml注入配置文件
@Component// 组件 相当于配置文件中 <bean id="user" class="当前注解的类"/>
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private Boolean happy;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
public Person() {
}
public Person(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists) {
this.name = name;
this.age = age;
this.happy = happy;
this.birth = birth;
this.maps = maps;
this.lists = lists;
}
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 getHappy() {
return happy;
}
public void setHappy(Boolean happy) {
this.happy = happy;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
'}';
}
}
2.3 在test测试中进行测试
@Autowired
private Person person;
@Test
void contextLoads2() {
System.out.println(person);
}
3.使用properties配置文件注入
3.1 创建person2.properties
name=zhang
age=18
3.2创建person2.java
package com.example.demo.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
//注入配置方法三:加载指定配置文件
@Component// 组件 相当于配置文件中 <bean id="user" class="当前注解的类"/>
@PropertySource(value = "classpath:person2.properties") //绑定Yaml
public class Person2 {
@Value("${name}")
private String name;
@Value("${age}")
private Integer age;
private Boolean happy;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
public Person2() {
}
public Person2(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists) {
this.name = name;
this.age = age;
this.happy = happy;
this.birth = birth;
this.maps = maps;
this.lists = lists;
}
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 getHappy() {
return happy;
}
public void setHappy(Boolean happy) {
this.happy = happy;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
'}';
}
}
3.3 测试
4. @ConfigurationProperties与@Value对比
- @ConfigurationProperties只需要写一次即可 , @Value则需要每个字段都添加
- 松散绑定:yml中写的last-name,这个和lastName是一样的, - 后面跟
着的字母默认是大写的。这就是松散绑定。 - JSR303数据校验 , 这个就是我们可以在字段是增加一层过滤器验证 , 可以保证数据的合法性
- 复杂类型封装,yml中可以封装对象 , 使用value就不支持
4.1 结论
如果我们在某个业务中,只需要获取配置文件中的某个值,可以使用一下 @value;其他情况用yaml