當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
005_Spring的属性注入
生活随笔
收集整理的這篇文章主要介紹了
005_Spring的属性注入
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. DI依賴注入, 前提必須有IOC的環(huán)境, Spring管理這個(gè)類的時(shí)候?qū)㈩惖囊蕾嚨膶傩宰⑷?設(shè)置)進(jìn)來。
2. Spring的屬性注入
2.1. 構(gòu)造方法的方式的屬性注入
2.2. Set方法的方式的屬性注入
2.3. 集合類型屬性注入
3. 依賴注入實(shí)例
3.1. 新建一個(gè)名為SpringDI的Java工程, 拷入Spring相關(guān)包
3.2. 創(chuàng)建Student.java
package com.lywgames.bean;public class Student {private int id;private String name;public Student(int id, String name) {this.id = id;this.name = 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;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + "]";}}3.3. 創(chuàng)建Teacher.java
package com.lywgames.bean;public class Teacher {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;}@Overridepublic String toString() {return "Teacher [id=" + id + ", name=" + name + "]";}}3.4. 創(chuàng)建Clazz.java
package com.lywgames.bean;import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set;public class Clazz {private Teacher teacher;private String[] clazzName;private List<Student> studentList;private Set<Student> studentSet;private Map<Integer, Student> studentMap;public Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;}public String[] getClazzName() {return clazzName;}public void setClazzName(String[] clazzName) {this.clazzName = clazzName;}public List<Student> getStudentList() {return studentList;}public void setStudentList(List<Student> studentList) {this.studentList = studentList;}public Set<Student> getStudentSet() {return studentSet;}public void setStudentSet(Set<Student> studentSet) {this.studentSet = studentSet;}public Map<Integer, Student> getStudentMap() {return studentMap;}public void setStudentMap(Map<Integer, Student> studentMap) {this.studentMap = studentMap;}@Overridepublic String toString() {return "Clazz [teacher=" + teacher + ", clazzName=" + Arrays.toString(clazzName) + ", studentList=" + studentList + ", studentSet=" + studentSet+ ", studentMap=" + studentMap + "]";}}3.5. 創(chuàng)建Test.java
package com.lywgames;import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lywgames.bean.Clazz; import com.lywgames.bean.Student; import com.lywgames.bean.Teacher;public class Test {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");System.out.println(context.getBean(Student.class).toString());System.out.println(context.getBean(Teacher.class).toString());System.out.println(context.getBean(Clazz.class).toString());context.close();} }3.6. 在src目錄下創(chuàng)建applicationContext.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"><!-- 構(gòu)造函數(shù)注入 --><bean id="student" class="com.lywgames.bean.Student"> <constructor-arg name="id" value="101"></constructor-arg><constructor-arg name="name" value="zhangsan"></constructor-arg></bean> <!-- set方法注入 --><bean id="teacher" class="com.lywgames.bean.Teacher"> <property name="id" value="901"></property><property name="name" value="xiaocui"></property></bean> <!-- 注入數(shù)組, list, set, map --><bean id="clazz" class="com.lywgames.bean.Clazz"> <property name="teacher" ref="teacher"></property><!-- 注入數(shù)組--><property name="clazzName"><list><value>計(jì)算機(jī)1班</value><value>三好班級(jí)</value></list></property><!-- 注入 list集合 --><property name="studentList"><list><ref bean="student"/><ref bean="student"/></list></property><!-- 注入set集合 --><property name="studentSet"><set><ref bean="student"/><ref bean="student"/></set></property><!-- 注入map集合 --><property name="studentMap"><map><entry key="801" value-ref="student"></entry><entry key="802" value-ref="student"></entry></map></property></bean> </beans>3.7. 運(yùn)行項(xiàng)目
總結(jié)
以上是生活随笔為你收集整理的005_Spring的属性注入的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 004_Bean标签
- 下一篇: 006_P名称空间的属性注入