千家信息网

Spring依赖注入多种类型数据的代码怎么写

发表于:2025-01-16 作者:千家信息网编辑
千家信息网最后更新 2025年01月16日,这篇文章主要介绍"Spring依赖注入多种类型数据的代码怎么写",在日常操作中,相信很多人在Spring依赖注入多种类型数据的代码怎么写问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希
千家信息网最后更新 2025年01月16日Spring依赖注入多种类型数据的代码怎么写

这篇文章主要介绍"Spring依赖注入多种类型数据的代码怎么写",在日常操作中,相信很多人在Spring依赖注入多种类型数据的代码怎么写问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Spring依赖注入多种类型数据的代码怎么写"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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 hobbies;    private Map cards;    private Set 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 getHobbies() {        return hobbies;    public void setHobbies(List hobbies) {        this.hobbies = hobbies;    public Map getCards() {        return cards;    public void setCards(Map cards) {        this.cards = cards;    public Set getGames() {        return games;    public void setGames(Set 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

                                                                                                                Java编程思想                MySQL必知必会                平凡的世界                                                                                                rap                打篮球                                                                                                                                                LOL                DNF                COC                                                                                                    root                123456                        

测试

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依赖注入多种类型数据的代码怎么写"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0