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

网友投稿 602 2022-10-14

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

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

目录Student实体类StudentsClass实体类beans.xml测试

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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

Java编程思想

mysql必知必会

平凡的世界

rap

打篮球

LOL

DNF

COC

root

123456

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

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

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring事务管理下synchronized锁失效问题的解决方法
下一篇:JDK8新特性_接口中也可以有方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~