当前位置: 首页 > news >正文

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

目录

  • 1. 使用@Value和@ConfigurationProperties
  • 2. 使用@PropertySource
      • 创建Person.java
      • 写一个测试类
  • 3. 使用@ImportResource
    • Student类
    • 创建beans.xml
    • 在主类中引入
    • 测试
  • 心得

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;}@Overridepublic 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 {@AutowiredPersonX personX;@Testpublic 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;}@Overridepublic 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 {@AutowiredStudent student;@Testpublic void print5(){System.out.println(student);}
}

运行结果:

Student{name='李四', age=18}

心得

平常还是要多多总结的。
在这里插入图片描述
谢谢大家支持!!!!

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • MySQL的InnoDB存储引擎
  • JS输出随机数
  • 2024关于日本AI 领域TOP12 的大学介绍
  • vector的底层原理剖析及其实现
  • 【Linux】一篇总结!什么是重定向?输出重定向的作用是什么?什么又是追加重定向?
  • 『C++实战项目 负载均衡式在线OJ』一、项目介绍与效果展示(持续更新)
  • python实现图像分割算法3
  • SuccBI+低代码文档中心 — 低代码应用(SuccAP)(概论)
  • Node.js(5)——包
  • SAP项目管理基础与实践第一章完结
  • ansible,laas,pass,sass
  • 《LlamaIndex 之美》-01-LLM、Prompt、Embedding基础入门
  • 【JavaEE】线程状态
  • Linux5:Shell编程——流程控制
  • 借助Aspose.html控件, 将SVG 转PNG 的 C# 图像处理库
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • 0x05 Python数据分析,Anaconda八斩刀
  • Android 控件背景颜色处理
  • CentOS7简单部署NFS
  • CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里...
  • JAVA SE 6 GC调优笔记
  • Java 内存分配及垃圾回收机制初探
  • Linux快速配置 VIM 实现语法高亮 补全 缩进等功能
  • 观察者模式实现非直接耦合
  • 实战|智能家居行业移动应用性能分析
  • 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿
  • 微服务框架lagom
  • 学习HTTP相关知识笔记
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • ​水经微图Web1.5.0版即将上线
  • #【QT 5 调试软件后,发布相关:软件生成exe文件 + 文件打包】
  • #define MODIFY_REG(REG, CLEARMASK, SETMASK)
  • #LLM入门|Prompt#1.8_聊天机器人_Chatbot
  • #单片机(TB6600驱动42步进电机)
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • (2)关于RabbitMq 的 Topic Exchange 主题交换机
  • (C语言)二分查找 超详细
  • (ISPRS,2021)具有遥感知识图谱的鲁棒深度对齐网络用于零样本和广义零样本遥感图像场景分类
  • (k8s)Kubernetes本地存储接入
  • (备份) esp32 GPIO
  • (附源码)小程序儿童艺术培训机构教育管理小程序 毕业设计 201740
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • (原創) 是否该学PetShop将Model和BLL分开? (.NET) (N-Tier) (PetShop) (OO)
  • (转载)虚函数剖析
  • **Java有哪些悲观锁的实现_乐观锁、悲观锁、Redis分布式锁和Zookeeper分布式锁的实现以及流程原理...
  • ... fatal error LINK1120:1个无法解析的外部命令 的解决办法
  • .form文件_一篇文章学会文件上传
  • .NET Core SkiaSharp 替代 System.Drawing.Common 的一些用法
  • .Net Core webapi RestFul 统一接口数据返回格式
  • .net 使用ajax控件后如何调用前端脚本
  • .vollhavhelp-V-XXXXXXXX勒索病毒的最新威胁:如何恢复您的数据?
  • .vue文件怎么使用_我在项目中是这样配置Vue的
  • @reference注解_Dubbo配置参考手册之dubbo:reference