Spring依赖注入通常有哪些实现方式,spring依赖注入的三种方式列举
目录
学生实体类学生班级实体类beans.xml测试
Student实体类
包实体;导入Java。util。*;/* * * * @作者乐道* @公司* @ create 2022-02-13 21:26 */公共课学生{私有int id私有字符串名称;私立学生班学生班;私串[]本书;私人列表字符串爱好;私有映射字符串、字符串卡;私人SetString游戏;私串老婆;私有属性信息;public int getId(){ return id;} public void setId(int id){ this。id=id公共字符串getName(){ return name;公共void集名称(字符串名称){ this。name=名称;公共学生类getStudentClass(){返回学生类;public void setStudentClass(学生类学生类){ this。学生班=学生班;public String[]get books(){ return books;public void setBooks(String[]books){ this。书籍=书籍;public list string get favorites(){ return favorites;public void set favorites(list string favorites){ this。收藏夹=收藏夹;public MapString,String get cards(){ return cards;公共void setCards(MapString,String cards){ this。卡片=卡片;public SetString get games(){ return games;公共void set games(set string games){ this。游戏=游戏;公共字符串getWife(){返回妻子;公空集妻(串妻){此。妻子=妻子;公共属性getInfo(){ return info;public void setInfo(属性信息){ this。info=info@ Override public String to String(){ return Student { id= id ,name= name ,studentClass= studentClass ,books= Arrays.toString(books),affairs= affairs ,cards= cards ,games= games ,wife= wife ,info= info }}
StudentsClass实体类
帕
ckage 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依赖注入内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。