在java文件夹中创建Person.java,Dog.java

Dog.java

package com.quan.spring02.pogo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//把下列代码加入Spring组件
@Component
public class Dog {
    @Value("wangcai")
    private String name;
    @Value("3")
    private Integer age;

    public Dog() {
    }

    public Dog(String name, Integer age) {
        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;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

在test中java文件测试

package com.quan.spring02;

import com.quan.spring02.pogo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Spring02ApplicationTests {

    @Autowired
    Dog dog;
    @Test
    void contextLoads() {

        System.out.println(dog);
    }

}

关于Error

在测试类中会有Dog画红线报错
could not autowire field xxxx
具体原因是没有引入import com.quan.spring02.pogo.Person;
打开 IDEA 的首选项,找到 Editor | General | Auto Import。勾选上 Add unambiguous imports on the fly 和 Optimize imports on the fly (for current project)。就可以实现自动导入。
如果不自动导入,只要按下 ⌥(option)和回车就可以接受IDEA建议。

然而@Value()的赋值方法已然过时

现如今使用yaml的方法实例化

Person.java

package com.quan.spring02.pogo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

//为了能被扫描到
@Component
@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;
    private Dog dog;


    public Person() {
    }

    public Person(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.birth = birth;
        this.maps = maps;
        this.lists = lists;
        this.dog = dog;
    }

    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;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", happy=" + happy +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

在test中java文件测试

package com.quan.spring02;

import com.quan.spring02.pogo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Spring02ApplicationTests {

    @Autowired
    Person person;
    @Test
    void contextLoads() {

        System.out.println(person);
    }

}

application.yaml

person:
  name: quanwu
  age: 20
  happy: false
  birth: 2000/01/08
  maps: {k1: v1,k2: v2}
  lists:
    - code
    - music
    - girl
  dog:
    name: wangcai
    age: 3

优点是

  • 批量注入
  • 支持松散绑定(松散语法
    带-与驼峰命名可以视作同一个类型
  • JSR303数据校验
    @Validated//数据校验……

      @Email()

Bean Validation 中内置的 constraint

Hibernate Validator 附加的 constraint


详细注解

  • 支持复杂型封装

拓展:
@ConfigurationProperties,它可以把同类的配置信息自动封装成实体类

清理maven报错Error executing Maven. The specified user settings file does not exist: /Use……
解决办法:在setting ->maven设置好User settings file!

Spring Boot全局配置文件中支持使用随机数和占位符的方式进行添加属性。
1、随机数

${random.value}

${random.int}

${random.int(max)}

${random.int(min,max)}

${random.long}

${random.long(max)}

${random.long(min,max)}

${random.uuid}

2、占位符
可以引用之前的属性值,如果没有则可以使用默认值。例如:${aaa: default},如果 ${aaa}不存在,则使用default代替aaa的值;如果${aaa}存在,则使用${aaa}的值。

a = ${random.int}
b = ${random.int(100)}
c = ${random.int(500,1000)}
d = ${random.long}
e = ${random.long(10000)}
f = ${random.long(20000,99999)}
g = ${random.value}
h = ${random.uuid}
i = ${aaa:"bbb"}

在解析占位符的时候,例如有个变量i = ${aaa:"bbb"},在引用i变量的时候,会先解析i的值,然后在解析${aaa:"bbb"}的值,接着又解析${aaa}的值,如果存在,则直接使用aaa变量的值,如果不存在,则直接使用"bbb"作为a变量的值,实际上是一种递归解析。

随机数及占位符参考链接:https://blog.csdn.net/caychen/article/details/79967979