springboot给类进行赋初值的四种方式

目录

1. 使用@Value和@ConfigurationProperties

这里不加赘述了,前面我也发过,这里就放个链接吧
@Value获取值和@ConfigurationProperties获取值用法及比较(springboot)

2. 使用@PropertySource

创建Person.java

java 复制代码
package com.example.springbootdaily2.model;

import org.springframework.format.annotation.DateTimeFormat;

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

@Component
@PropertySource(value = "classpath:person.properties")
// 这个是前缀的意思
@ConfigurationProperties(prefix = "person2")
public class PersonX {
    private String name;
    private Character sex;
    @DateTimeFormat(pattern = "YYYY-MM-SS")
    private Date birthday;
    private Integer age;
    private String address;
    private Map<String, Integer> maps;
    private List<String> lists;
    private Dog dog;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Character getSex() {
        return sex;
    }

    public void setSex(Character sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Dog getDog() {
        return dog;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Map<String, Integer> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Integer> maps) {
        this.maps = maps;
    }

    public List<String> getLists() {
        return lists;
    }

    public void setLists(List<String> lists) {
        this.lists = lists;
    }

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

创建person.properties

java 复制代码
person2.name="李四"
person2.sex=男
person2.birthday=2022-02-07
person2.age=18
person2.maps.keys1=16
person2.maps.keys2=16
person2.lists=[12,24,57]
person2.address="保定廉耻"
person2.dog.name=${random.value}

写一个测试类

java 复制代码
package com.example.springbootdaily;
import com.example.springbootdaily.model.Dog;
import com.example.springbootdaily.model.Person;
import com.example.springbootdaily.model.Person2;
import com.example.springbootdaily.model.PersonX;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringTest {
    @Autowired
    PersonX personX;

    @Test
    public void print4(){
        System.out.println(personX);
    }
}

输出结果:

java 复制代码
Person{name='"岳轩子"', sex=M, 
birthday=Sun Dec 26 00:00:00 CST 2021, age=18, 
address='"保定武汉"', maps={keys2=16, keys1=16}, lists=[[12, 24, 57]], 
dog=Dog{name='cdab390f55c9f8a6bbb420cd15607add'}}

注:如果显示乱码,设置文件编码为utf-8

3. 使用@ImportResource

Student类

java 复制代码
package com.example.springbootdaily.model;

public class Student {
    private String name;
    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;
    }

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

创建beans.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="student" class="com.example.springbootdaily.model.Student">
        <property name="name" value="李四"/>
        <property name="age" value="18"/>
    </bean>
</beans>

在主类中引入

java 复制代码
package com.example.springbootdaily;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(locations = "classpath:beans.xml")
public class SpringbootDailyApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDailyApplication.class, args);
    }

}

测试

java 复制代码
package com.example.springbootdaily;


import com.example.springbootdaily.model.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringTest {
    @Autowired
    Student student;

    @Test
    public void print5(){
        System.out.println(student);
    }
}

运行结果:

复制代码
Student{name='李四', age=18}

其他

我们可以导入配置文件处理器,以后编写配置就有提示了

<!‐‐导入配置文件处理器,配置文件进行绑定就会有提示‐‐>

依赖:

xml 复制代码
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring‐boot‐configuration‐processor</artifactId>
     <optional>true</optional>
</dependency>

心得

平常还是要多多总结的。

谢谢大家支持!!!!

相关推荐
少许极端6 小时前
算法奇妙屋(六)-哈希表
java·数据结构·算法·哈希算法·散列表·排序
Da Da 泓6 小时前
shellSort
java·数据结构·学习·算法·排序算法
武子康6 小时前
Java-148 深入浅出 MongoDB 聚合操作:$match、$group、$project、$sort 全面解析 Pipeline 实例详解与性能优化
java·数据库·sql·mongodb·性能优化·系统架构·nosql
珹洺6 小时前
Java-Spring入门指南(二十四)SSM整合HTML:解决CSS/JS静态资源被过滤问题
java·spring·html
IT_陈寒6 小时前
Java性能调优实战:7个让GC效率提升50%的关键参数设置
前端·人工智能·后端
莫陌尛.6 小时前
SSM(Spring+SpringMVC+Mybatis)整合
java·spring·mybatis
ANYOLY7 小时前
多线程&并发篇面试题
java·面试
Apifox7 小时前
如何在 Apifox 中通过 AI 一键生成几十个测试用例?
前端·后端·ai编程
用户298698530147 小时前
Java高效读取CSV文件的方法与分步实例
java·后端
一根甜苦瓜7 小时前
Go语言Slice的一道骚题
开发语言·后端·golang