DI注入:給參數指派的過程
(1)Setter注入
班級和學生
建立兩個實體類
1.學生類
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Student{
private Integer id;
private String name;
private Classes cls;
private List list;//選修課
private Set set;
private Map map;
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setCls(Classes cls) {
this.cls = cls;
}
public void setList(List list) {
this.list = list;
}
public void setSet(Set set) {
this.set = set;
}
public void setMap(Map map) {
this.map = map;
}
public void method() {
System.out.println("Student [id=" + id + ", name=" + name + ", cls=" + cls
+ ", list=" + list + ", set=" + set + ", map=" + map + "]");
}
}
班級
public class Classes {
}
2.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">
<bean id="cls" class="com.xx.entity.Classes"/>
<bean id="stu" class="com.xx.entity.Student">
<!--1.普通類型注入 -->
<property name="id" value="11212121"/>
<property name="name" value="小明"/>
<!--2.類類型注入 -->
<property name="cls" ref="cls"/>
<!--3.集合類注入 -->
<property name="list">
<list>
<value>大學資料</value>
<value>資料原理</value>
</list>
</property>
<property name="set">
<set>
<value>随便1</value>
<value>随便2</value>
</set>
</property>
<property name="map">
<map>
<entry key="01" value="1001"/>
<entry key="02" value="1002"/>
</map>
</property>
</bean>
</beans>
3.測試類
package com.xx.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xx.entity.Student;
public class TestSpring {
@Test
public void testConn(){
//1.初始化Spring容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.提取bean,根據容器中的id來擷取的
Student stu=(Student) ctx.getBean("stu");
//3.調方法
stu.method();
}
}
(2)通過構造器注入
1.建立兩個實體類
Student
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Student{
private Integer id;
private String name;
private Classes cls;
public Student(Integer id, String name, Classes cls) {
super();
this.id = id;
this.name = name;
this.cls = cls;
}
public void method() {
System.out.println("Student [id=" + id + ", name=" + name + ", cls=" + cls + "]");
}
}
Classess
public class Classes {
}
2.applicationContext.xml
通過下标注入
<bean id="stu" class="com.xx.entity.Student">
<constructor-arg index="0" value="1001"/>
<constructor-arg index="1" value="小明"/>
<constructor-arg index="2" ref="cls"/>
</bean>
通過資料類型指派
<bean id="stu" class="com.xx.entity.Student">
<constructor-arg type="java.lang.Integer" value="1001"/>
<constructor-arg type="java.lang.String" value="小明"/>
<constructor-arg type="com.xx.entity.Classes" ref="cls"/>
</bean>
總
<?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="cls" class="com.xx.entity.Classes"/>
<!-- <bean id="stu" class="com.xx.entity.Student">
<constructor-arg index="0" value="1001"/>
<constructor-arg index="1" value="小明"/>
<constructor-arg index="2" ref="cls"/>
</bean> -->
<bean id="stu" class="com.xx.entity.Student">
<constructor-arg type="java.lang.Integer" value="1001"/>
<constructor-arg type="java.lang.String" value="小明"/>
<constructor-arg type="com.xx.entity.Classes" ref="cls"/>
</bean>
</beans>
3.測試類
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lanou.entity.Student;
public class TestSpring {
@Test
public void testConn(){
//1.初始化Spring容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.提取bean,根據容器中的id來擷取的
Student stu=(Student) ctx.getBean("stu");
//3.調方法
stu.method();
}
}
(3)通過注解注入
如果寫了注解,就不用寫配置檔案了.隻需要在xml中開啟注解,限制檔案也需要重新配置.
找到源碼包下的
spring-framework-4.3.16.RELEASE-dist/spring-framework-4.3.16.RELEASE/docs/spring-framework-reference/html/xsd-configuration.html
找到上下文的執行個體,将給出的執行個體複制到xml中
the context schema
Person類
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component //<bean id="person" class="com.xx.entity.Person">
public class Person{//bean中id自動解析成類名首字母小寫
//注入Student對象
//@Autowired //ref引入student對象
@Resource //ref引入student對象,省略name=“stu”
private Student stu;
public void say() {
System.out.println("helloStudent...");
}
public Student getStu() {
return stu;
}
//<property name="stu">
public void setStu(Student stu) {
this.stu = stu;
}
Student類
import org.springframework.stereotype.Component;
@Component//<bean id="student" class="com.xx.entity.Student"/>
public class Student{
}
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"
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--開啟注解配置 -->
<context:annotation-config/>
<!--目前掃描包 -->
<context:component-scan base-package="com.xx"/>
</beans>