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}
其他
我们可以导入配置文件处理器,以后编写配置就有提示了
<!‐‐导入配置文件处理器,配置文件进行绑定就会有提示‐‐>
依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring‐boot‐configuration‐processor</artifactId><optional>true</optional>
</dependency>
心得
平常还是要多多总结的。

谢谢大家支持!!!!
相关文章:
springboot给类进行赋初值的四种方式
目录 1. 使用Value和ConfigurationProperties2. 使用PropertySource创建Person.java写一个测试类 3. 使用ImportResourceStudent类创建beans.xml在主类中引入测试 其他心得 1. 使用Value和ConfigurationProperties 这里不加赘述了,前面我也发过,这里就放…...
Day32 | 1049. 最后一块石头的重量 II 494. 目标和 474.一和零
语言 Java 1049. 最后一块石头的重量 II 最后一块石头的重量 II 题目 有一堆石头,用整数数组 stones 表示。其中 stones[i] 表示第 i 块石头的重量。 每一回合,从中选出任意两块石头,然后将它们一起粉碎。假设石头的重量分别为 x 和 …...
linux 查看一个端口是否被占用
1 linux命令 要在Linux中查看一个端口是否被占用,可以按照以下步骤进行操作: 打开终端(Terminal)。 运行以下命令来列出系统上所有正在监听的端口及其对应的进程: sudo netstat -tuln | grep LISTEN这将显示所有正在…...
【Git】5. 配置 Git
配置.gitignore – 忽略特殊⽂件 在⽇常开发中,我们有些⽂件不想或者不应该提交到远端,⽐如保存了数据库密码的配置⽂件,那怎么让 Git 知道呢? 在 Git ⼯作区的根⽬录下创建⼀个特殊的 .gitignore ⽂件,然后把要忽略的…...
C语言:文件处理
文件处理 一、文件的类型(一)文本文件和二进制文件 (二)程序文件和数据文件数据文件按照二进制储存 二、文件的打开和关闭(一)文件指针(二)文件的打开和关闭1、fopen2、fclose &…...
SpringBoot MybatisPlus selectOne的坑
目录 一、问题 二、问题解决 三、其他方法 一、问题 selectOne在查询多条数据时会报错,查询语句并不会加 limit 1。 One record is expected, but the query result is multiple records。 二、问题解决 在QueryWrapper上添加如下: QueryWrapper&…...
Spring源码-ClassPathXmlApplicationContext的refresh()都做了什么?
AbstractApplicationContext的refresh方法 /*** 用给定的父类创建一个新的ClassPathXmlApplicationContext* Create a new ClassPathXmlApplicationContext with the given parent,* 从给定的XML文件加载定义* loading the definitions from the given XML files.* param confi…...
网站加密和混淆技术简介
我们在爬取网站的时候,会遇到一些需要分析接口或 URL 信息的情况,这时会有各种各样类似加密的情况 1. 某个网站的URL 带有一些看不懂的长串加密字符,要抓取就必须懂的这些参数是怎么构造的,否则我们连完整的 URL 都构造不出来&am…...
Kafka + Kraft 集群搭建教程,附详细配置及自动化安装脚本
本文主要介绍 kafka kraft 搭建过程,主要用途是为了日志采集,所以搭建相对比较简单暴力,不过也可以作为一个参考供大家学习,主打一个能用管跑(调优啊,参数解释啊,原理啊,太枯燥了&a…...
“Apple Intelligence”的“系统提示词”被曝光了
当 苹果的 Apple Intelligence 还未完全开放体验时,其提示词就已经曝光了。 苹果如何指挥 AI 干活,这次被泄露的非常彻底。我们就拿邮件来说,借助 AI,收发及回复邮件变得非常简单,但背后的逻辑是内置提示词在拿捏。 比…...
django学习-数据表操作
一、数据表操作 1. 数据新增 由模型实例化对象调用内置方法实现数据新增,比如单数据新增调用create,查询与新增调用get_or_create,修改与新增调用update_or_create,批量新增调用bulk_create。 1.1 create() # 方法一 # 使用cr…...
机器学习-决策树
决策树 决策树1. 简介2. ID3 决策树3. C4.5决策树4. CART决策树5. 决策树对比6. 正则化 剪枝 决策树 1. 简介 """ 简介一种树形结构树中每个内部节点表示一个特征的判断每个分支代表一个判断结果的输出每个叶节点代表一种分类结果建立过程1. 特征选择选取有较…...
opencascade TopoDS_Shape源码学习【重中之重】
opencascade TopoDS_Shape 前言 描述了一个形状,它 引用了一个基础形状,该基础形状有可能被赋予一个位置和方向 为基础形状提供了一个位置,定义了它在本地坐标系中的位置为基础形状提供了一个方向,这是从几何学的角度ÿ…...
Self-study Python Fish-C Note15 P52to53
函数 (part 5) 本节主要讲函数文档、类型注释、内省、高阶函数 函数文档、类型注释、内省 (P52) 函数文档 函数是一种代码封装的方法,对于一个程序来说,函数就是一个结构组件。在函数的外部是不需要关心函数内部的执行细节的,更需要关注的…...
Java小白入门到实战应用教程-异常处理
Java小白入门到实战应用教程-异常处理 前言 我们这一章节进入到异常处理知识点的学习。异常是指程序在运行时遇到的一种特殊情况,它能打断了正常的程序执行流程。 而异常处理是一项至关重要的技术,它使得程序能够优雅地处理运行时错误,避免…...
使用Anaconda安装多个版本的Python并与Pycharm进行对接
1、参考链接 Anaconda安装使用教程解决多Python版本问题_anaconda安装多个python版本-CSDN博客 基于上面的一篇博客的提示,我做了尝试。并在Pycharm的对接上做了拓展。 2、首先安装Anaconda 这个比较简单,直接安装即可: 3、设置conda.exe的…...
android系统中data下的xml乱码无法查看问题剖析及解决方法
背景: Android12高版本以后系统生成的很多data路径下的xml都变成了二进制类型,根本没办法看xml的内容具体如下: 比如想要看当前系统的widget的相关数据 ./system/users/0/appwidgets.xml 以前老版本都是可以直接看的,这些syste…...
MySQL——索引(三)创建索引(2)使用 CREATE INDEX 语句在已经存在的表上创建索引
若想在一个已经存在的表上创建索引,可以使用 CREATE INDEX.语句,CREATEINDEX语句创建索引的具体语法格式如下所示: CREATE [UNIQUEIFULLTEXTISPATIAL]INDEX 索引名 ON 表名(字段名[(长度)J[ASCIDESC]); 在上述语法格式中,UNIQUE、FULLTEXT 和…...
html+css 实现hover选择按钮
前言:哈喽,大家好,今天给大家分享htmlcss 绚丽效果!并提供具体代码帮助大家深入理解,彻底掌握!创作不易,如果能帮助到大家或者给大家一些灵感和启发,欢迎收藏关注哦 💕 目…...
Python数据可视化利器:Matplotlib详解
目录 Matplotlib简介安装MatplotlibMatplotlib基本用法 简单绘图子图和布局图形定制 常见图表类型 折线图柱状图散点图直方图饼图 高级图表和功能 3D绘图热图极坐标图 交互和动画与其他库的集成 与Pandas集成与Seaborn集成 常见问题与解决方案总结 Matplotlib简介 Matplotli…...
零门槛NAS搭建:WinNAS如何让普通电脑秒变私有云?
一、核心优势:专为Windows用户设计的极简NAS WinNAS由深圳耘想存储科技开发,是一款收费低廉但功能全面的Windows NAS工具,主打“无学习成本部署” 。与其他NAS软件相比,其优势在于: 无需硬件改造:将任意W…...
docker详细操作--未完待续
docker介绍 docker官网: Docker:加速容器应用程序开发 harbor官网:Harbor - Harbor 中文 使用docker加速器: Docker镜像极速下载服务 - 毫秒镜像 是什么 Docker 是一种开源的容器化平台,用于将应用程序及其依赖项(如库、运行时环…...
云计算——弹性云计算器(ECS)
弹性云服务器:ECS 概述 云计算重构了ICT系统,云计算平台厂商推出使得厂家能够主要关注应用管理而非平台管理的云平台,包含如下主要概念。 ECS(Elastic Cloud Server):即弹性云服务器,是云计算…...
dedecms 织梦自定义表单留言增加ajax验证码功能
增加ajax功能模块,用户不点击提交按钮,只要输入框失去焦点,就会提前提示验证码是否正确。 一,模板上增加验证码 <input name"vdcode"id"vdcode" placeholder"请输入验证码" type"text&quo…...
srs linux
下载编译运行 git clone https:///ossrs/srs.git ./configure --h265on make 编译完成后即可启动SRS # 启动 ./objs/srs -c conf/srs.conf # 查看日志 tail -n 30 -f ./objs/srs.log 开放端口 默认RTMP接收推流端口是1935,SRS管理页面端口是8080,可…...
Java-41 深入浅出 Spring - 声明式事务的支持 事务配置 XML模式 XML+注解模式
点一下关注吧!!!非常感谢!!持续更新!!! 🚀 AI篇持续更新中!(长期更新) 目前2025年06月05日更新到: AI炼丹日志-28 - Aud…...
PL0语法,分析器实现!
简介 PL/0 是一种简单的编程语言,通常用于教学编译原理。它的语法结构清晰,功能包括常量定义、变量声明、过程(子程序)定义以及基本的控制结构(如条件语句和循环语句)。 PL/0 语法规范 PL/0 是一种教学用的小型编程语言,由 Niklaus Wirth 设计,用于展示编译原理的核…...
QT: `long long` 类型转换为 `QString` 2025.6.5
在 Qt 中,将 long long 类型转换为 QString 可以通过以下两种常用方法实现: 方法 1:使用 QString::number() 直接调用 QString 的静态方法 number(),将数值转换为字符串: long long value 1234567890123456789LL; …...
招商蛇口 | 执笔CID,启幕低密生活新境
作为中国城市生长的力量,招商蛇口以“美好生活承载者”为使命,深耕全球111座城市,以央企担当匠造时代理想人居。从深圳湾的开拓基因到西安高新CID的战略落子,招商蛇口始终与城市发展同频共振,以建筑诠释对土地与生活的…...
华为OD机考-机房布局
import java.util.*;public class DemoTest5 {public static void main(String[] args) {Scanner in new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextLine()) { // 注意 while 处理多个 caseSystem.out.println(solve(in.nextLine()));}}priv…...
