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

 更新时间:2024年08月08日 09:56:55   作者:岳轩子  
这篇文章主要介绍了springboot给类进行赋初值的四种方式,并通过代码示例给大家介绍的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下

1. 使用@Value和@ConfigurationProperties

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

2. 使用@PropertySource

创建Person.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

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}

写一个测试类

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

输出结果:

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类

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

在主类中引入

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

}

测试

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}

其他

我们可以导入配置文件处理器,以后编写配置就有提示了
<!‐‐导入配置文件处理器,配置文件进行绑定就会有提示‐‐>
依赖:

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring‐boot‐configuration‐processor</artifactId>
     <optional>true</optional>
</dependency>

以上就是SpringBoot给类进行赋初值的四种方式的详细内容,更多关于SpringBoot给类进行赋初值的资料请关注脚本之家其它相关文章!

相关文章

  • Java实现读取和写入properties文件

    Java实现读取和写入properties文件

    这篇文章主要介绍了Java实现读取和写入properties文件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • java软引用在浏览器使用实例讲解

    java软引用在浏览器使用实例讲解

    在本篇文章里小编给大家整理的是一篇关于java软引用在浏览器使用实例讲解内容,有兴趣的朋友们可以学习下。
    2021-04-04
  • MyBatis-Plus使用动态表名分表查询的实现

    MyBatis-Plus使用动态表名分表查询的实现

    本文主要介绍了MyBatis-Plus使用动态表名分表查询,主要是动态修改表名的几种常见场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-11-11
  • 深入理解final变量的初始化

    深入理解final变量的初始化

    本篇文章是对final变量的初始化进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • 教你使用eclipse 搭建Swt 环境的全过程

    教你使用eclipse 搭建Swt 环境的全过程

    本文给大家分享使用eclipse 搭建Swt 环境的全过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-12-12
  • Java跨域问题的处理详解

    Java跨域问题的处理详解

    这篇文章主要给大家介绍了关于Java跨域问题处理的相关资料,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-03-03
  • java版十大排序经典算法:完整代码(4)

    java版十大排序经典算法:完整代码(4)

    优秀的文章也不少,但是Java完整版的好像不多,我把所有的写一遍巩固下,同时也真诚的希望阅读到这篇文章的小伙伴们可以自己去从头敲一遍,不要粘贴复制!希望我的文章对你有所帮助,每天进步一点点
    2021-07-07
  • 解决java 命令行乱码的问题

    解决java 命令行乱码的问题

    这篇文章主要介绍了解决java 命令行乱码的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • Mybatis自定义SQL的关系映射、分页、排序功能的实现

    Mybatis自定义SQL的关系映射、分页、排序功能的实现

    这篇文章主要介绍了Mybatis自定义SQL的关系映射、分页、排序功能的实现,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • 教你怎么通过IDEA设置堆内存空间

    教你怎么通过IDEA设置堆内存空间

    这篇文章主要介绍了教你怎么通过IDEA设置堆内存空间,文中有非常详细的代码示例,对正在使用IDEA的小伙伴们很有帮助哟,需要的朋友可以参考下
    2021-05-05

最新评论