Spring依赖注入多种类型数据的示例代码


Posted in Java/Android onMarch 31, 2022

Student实体类

package entity;
import java.util.*;
/**
 * @author LeDao
 * @company
 * @create 2022-02-13 21:26
 */
public class Student {
    private int id;
    private String name;
    private StudentClass studentClass;
    private String[] books;
    private List<String> hobbies;
    private Map<String, String> cards;
    private Set<String> games;
    private String wife;
    private Properties info;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    public String getName() {
        return name;
    public void setName(String name) {
        this.name = name;
    public StudentClass getStudentClass() {
        return studentClass;
    public void setStudentClass(StudentClass studentClass) {
        this.studentClass = studentClass;
    public String[] getBooks() {
        return books;
    public void setBooks(String[] books) {
        this.books = books;
    public List<String> getHobbies() {
        return hobbies;
    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    public Map<String, String> getCards() {
        return cards;
    public void setCards(Map<String, String> cards) {
        this.cards = cards;
    public Set<String> getGames() {
        return games;
    public void setGames(Set<String> games) {
        this.games = games;
    public String getWife() {
        return wife;
    public void setWife(String wife) {
        this.wife = wife;
    public Properties getInfo() {
        return info;
    public void setInfo(Properties info) {
        this.info = info;
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", studentClass=" + studentClass +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", cards=" + cards +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
}

StudentsClass实体类

package entity;
/**
 * @author LeDao
 * @company
 * @create 2022-02-14 14:11
 */
public class StudentClass {
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Class{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

beans.xml

下面展示的数据类型有:一般类型、对象、数组、List、Map、Set、空值、Properties

<?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="studentClass1" class="entity.StudentClass">
        <property name="id" value="1"/>
        <property name="name" value="软件工程3班"/>
    </bean>
    <bean id="user1" class="entity.Student">
        <!--一般类型-->
        <property name="id" value="1"/>
        <property name="name" value="tom"/>
        <!--对象-->
        <property name="studentClass" ref="studentClass1"/>
        <!--数组-->
        <property name="books">
            <array>
                <value>Java编程思想</value>
                <value>MySQL必知必会</value>
                <value>平凡的世界</value>
            </array>
        </property>
        <!--List-->
        <property name="hobbies">
            <list>
                <value>唱</value>
                <value>跳</value>
                <value>rap</value>
                <value>打篮球</value>
            </list>
        </property>
        <!--Map-->
        <property name="cards">
            <map>
                <entry key="身份证" value="123"/>
                <entry key="校园卡" value="321"/>
            </map>
        </property>
        <!--Set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>DNF</value>
                <value>COC</value>
            </set>
        </property>
        <!--空值-->
        <property name="wife">
            <null/>
        </property>
        <!--Properties-->
        <property name="info">
            <props>
                <prop key="userName">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>
    </bean>
</beans>

测试

import config.MyConfig;
import entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @author LeDao
 * @company
 * @create 2022-02-12 15:56
 */
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("user1");
        System.out.println(student);
    }
}

到此这篇关于Spring依赖注入多种类型数据的文章就介绍到这了,更多相关Spring依赖注入内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Java/Android 相关文章推荐
为什么在foreach循环中JAVA集合不能添加或删除元素
Jun 11 Java/Android
springboot 启动如何排除某些bean的注入
Aug 02 Java/Android
Java org.w3c.dom.Document 类方法引用报错
Aug 07 Java/Android
Java Spring 控制反转(IOC)容器详解
Oct 05 Java/Android
关于Spring配置文件加载方式变化引发的异常详解
Jan 18 Java/Android
Android基于Fresco实现圆角和圆形图片
Apr 01 Java/Android
Spring Boot 实现 WebSocket
Apr 30 Java/Android
Android Canvas绘制文字横纵向对齐
Jun 05 Java/Android
解决spring.thymeleaf.cache=false不起作用的问题
Jun 10 Java/Android
SpringCloud中分析讲解Feign组件添加请求头有哪些坑梳理
Jun 21 Java/Android
一文搞懂Java中的注解和反射
Jun 21 Java/Android
Spring中bean集合注入的方法详解
Jul 07 Java/Android
springboot layui hutool Excel导入的实现
spring注解 @PropertySource配置数据源全流程
Mar 25 #Java/Android
Netty客户端接入流程NioSocketChannel创建解析
Mar 25 #Java/Android
Java 超详细讲解设计模式之中的抽象工厂模式
Netty分布式客户端处理接入事件handle源码解析
Java 超详细讲解IO操作字节流与字符流
Netty分布式客户端接入流程初始化源码分析
Mar 25 #Java/Android
You might like
初学CAKEPHP 基础教程
2009/11/02 PHP
Zend Framework教程之路由功能Zend_Controller_Router详解
2016/03/07 PHP
Yii rules常用规则示例
2016/03/15 PHP
PHP学习笔记之php文件操作
2016/06/03 PHP
Yii2中SqlDataProvider用法示例
2016/09/22 PHP
php实现背景图上添加圆形logo图标的方法
2016/11/17 PHP
php实现查询功能(数据访问)
2017/05/23 PHP
html向js方法传递参数具体实现
2013/08/08 Javascript
jQuery AJAX timeout 超时问题详解
2016/06/21 Javascript
js获取html的span标签的值方法(超简单)
2016/07/26 Javascript
JS笛卡尔积算法与多重数组笛卡尔积实现方法示例
2017/12/01 Javascript
使用classList来实现两个按钮样式的切换方法
2018/01/24 Javascript
JS实现图片转换成base64的各种应用场景实例分析
2018/06/22 Javascript
解决vue 绑定对象内点击事件失效问题
2018/09/05 Javascript
利用JavaScript为句子加标题的3种方法示例
2021/01/05 Javascript
Python学习之asyncore模块用法实例教程
2014/09/29 Python
Python中isnumeric()方法的使用简介
2015/05/19 Python
简介二分查找算法与相关的Python实现示例
2015/08/26 Python
实例详解Matlab 与 Python 的区别
2019/04/26 Python
Python搭建代理IP池实现检测IP的方法
2019/10/27 Python
Pytorch根据layers的name冻结训练方式
2020/01/06 Python
基于Python的Jenkins的二次开发操作
2020/05/12 Python
Python基于jieba, wordcloud库生成中文词云
2020/05/13 Python
Python虚拟环境的创建和包下载过程分析
2020/06/19 Python
Python字典fromkeys()方法使用代码实例
2020/07/20 Python
详解基于Scrapy的IP代理池搭建
2020/09/29 Python
css3实现椭圆轨迹旋转的示例代码
2018/10/29 HTML / CSS
夏威夷航空官网:Hawaiian Airlines
2016/09/11 全球购物
百度JavaScript笔试题
2015/01/15 面试题
创业培训计划书
2014/05/03 职场文书
理财计划书
2014/08/14 职场文书
社区精神文明建设汇报材料
2014/08/17 职场文书
小学科学教学计划
2015/01/21 职场文书
2015年清明节活动总结
2015/02/09 职场文书
上级领导检查欢迎词
2015/09/30 职场文书
PHP中多字节字符串操作实例详解
2021/08/23 PHP