java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot配置文件给bean赋值

SpringBoot如何配置文件给bean赋值问题

作者:键盘都敲坏了

这篇文章主要介绍了SpringBoot如何配置文件给bean赋值问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

方式一、使用yaml文件对bean进行注入

案例

(先来两个实体类做为案例测试用)

 
@Component
public class Car {
 
    String name;
    String pinpai;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getPinpai() {
        return pinpai;
    }
 
    public void setPinpai(String pinpai) {
        this.pinpai = pinpai;
    }
 
    public Car(String name, String pinpai) {
        this.name = name;
        this.pinpai = pinpai;
    }
 
    public Car() {
    }
 
    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", pinpai='" + pinpai + '\'' +
                '}';
    }
}
//此注解通过application.yaml里的person来注入参数
@ConfigurationProperties(prefix = "person")
@Component
@ConfigurationProperties(prefix = "person") //通过application.yaml里的person来注入参数
public class Person {
 
     String name;
    String age;
    String idcard;
    Map map;
    List list;
    Car car;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getAge() {
        return age;
    }
 
    public void setAge(String age) {
        this.age = age;
    }
 
    public String getIdcard() {
        return idcard;
    }
 
    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }
 
    public Map getMap() {
        return map;
    }
 
    public void setMap(Map map) {
        this.map = map;
    }
 
    public List getList() {
        return list;
    }
 
    public void setList(List list) {
        this.list = list;
    }
 
    public Car getCar() {
        return car;
    }
 
    public void setCar(Car car) {
        this.car = car;
    }
 
    public Person(String name, String age, String idcard, Map map, List list, Car car) {
        this.name = name;
        this.age = age;
        this.idcard = idcard;
        this.map = map;
        this.list = list;
        this.car = car;
    }
 
    public Person() {
    }
 
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", idcard='" + idcard + '\'' +
                ", map=" + map +
                ", list=" + list +
                ", car=" + car +
                '}';
    }
}

application.yaml里的代码:

(注意一点的是yaml文件必须以application命名才能被springboot识别)

person:
  name: zhangsan
  age: 20
  idcard: 342115454
  map: {key: 1,key2: 2}
  list:
    - java
    - html
    - js
    - css
  car:
    name: 21款小奔奔
    pinpai: 国产汽车

测试输出的信息为:

person{
    name='zhangsan', age='20', idcard='342115454', 
    map={key=1, key2=2}, list=[java, html, js, css],
    car=Car{name='21款小奔奔', pinpai='国产汽车'}
}

甚至还能玩的更花样一点:

person:
  name: zhangsan
  #在20后面加上随机的int整数
  age: 20${random.int}
  #随机生成的long的整数
  idcard: ${random.long}
  map: {key: 1,key2: 2}
  check: 大奔奔汽车
  list:
    - java
    - html
    - js
    - css
  car:
    name: 21款小奔奔
    #判断person中的key名为check的值是否存在,如果存在则注入person.check的值,不存在则注入小奔奔汽车
    pinpai: ${person.check:小奔奔汽车}

方式二、使用自定义配置文件对bean进行注入

案例

(还是先来两个实体类做为案例测试用)

@Component
public class Car1 {
 
    String name;
    String pinpai;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getPinpai() {
        return pinpai;
    }
 
    public void setPinpai(String pinpai) {
        this.pinpai = pinpai;
    }
 
    public Car1(String name, String pinpai) {
        this.name = name;
        this.pinpai = pinpai;
    }
 
    public Car1() {
    }
 
    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", pinpai='" + pinpai + '\'' +
                '}';
    }
}
//javaConfig 绑定配置文件的值
//加载指定路径的配置文件  myconfig.properties
@PropertySource(value = "classpath:myconfig.properties")

再通过@Value("${key名}")来注入 ,key名则是配置文件中的key

@Component
//javaConfig 绑定配置文件的值
//加载指定配置文件
@PropertySource(value = "classpath:myconfig.properties")
public class Person1 {
 
    //SPEL表达式取出值
    @Value("${name}")
    String name;
    String age;
    String idcard;
    Map map;
    List list;
    Car1 car1;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getAge() {
        return age;
    }
 
    public void setAge(String age) {
        this.age = age;
    }
 
    public String getIdcard() {
        return idcard;
    }
 
    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }
 
    public Map getMap() {
        return map;
    }
 
    public void setMap(Map map) {
        this.map = map;
    }
 
    public List getList() {
        return list;
    }
 
    public void setList(List list) {
        this.list = list;
    }
 
    public Car1 getCar() {
        return car1;
    }
 
    public void setCar(Car1 car1) {
        this.car1 = car1;
    }
 
    public Person1(String name, String age, String idcard, Map map, List list, Car1 car1) {
        this.name = name;
        this.age = age;
        this.idcard = idcard;
        this.map = map;
        this.list = list;
        this.car1 = car1;
    }
 
    public Person1() {
    }
 
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", idcard='" + idcard + '\'' +
                ", map=" + map +
                ", list=" + list +
                ", car=" + car1 +
                '}';
    }
}

自定义配置文件myconfig.properties的代码:

只做其中两个参数的案例

name=zhangsan
age=22

测试输出的信息为:

Person{name='zhangsan', age='22', idcard='null', map=null, list=null, car=null}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文