DI:依赖注入:
给属性赋值,提供了(XML)两种方案给属性赋值
方式:
1.Setter方法
2.构造函数
public class Person{
private Long pid;
private String name;
//setter和getter方法
}
名词解释:
在Spring容器中给对象属性赋值就叫装配
DI注入就是给属性赋值的过程
1.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-2.5.xsd"> <bean id="person" class="com.spring.di.xml.setter.Person"> <!-- property就是一个bean的属性 name:用来描述属性的名称 value就是值,如果是一般类型(基本类型和Stirng) --> <property name="pid" value="1"></property> <property name="name" value="狗蛋"></property> <!-- Spring容器内部创建的student对象给person的student对象赋值了 --> <property name="student"> <ref bean="student"/> </property> <property name="list"> <list> <value>list1</value> <value>list2</value> <value>list3</value> <ref bean="student"/> </list> </property> <property name="sets"> <set> <value>set1</value> <value>set1</value> <ref bean="student"/> </set> </property> <property name="map"> <map> <entry key="m1"> <value>map1</value> </entry> <entry key="m2"> <ref bean="student"/> </entry> </map> </property> <property name="properties"> <props> <prop key="p1">prop1</prop> <prop key="p2">prop2</prop> </props> </property> <property name="objects"> <list> <value>obj1</value> <ref bean="student"/> </list> </property> </bean> <bean id="student" class="com.spring.di.xml.setter.Student"></bean> </beans>
测试代码
Person
1 package com.spring.di.xml.setter; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Properties; 6 import java.util.Set; 7 8 public class Person { 9 10 private Long pid; 11 private String name; 12 private Student student; 13 private List list; 14 private Set sets; 15 private Map map; 16 private Properties properties; 17 private Object[] objects; 18 19 public Long getPid() { 20 return pid; 21 } 22 public void setPid(Long pid) { 23 this.pid = pid; 24 } 25 public String getName() { 26 return name; 27 } 28 public void setName(String name) { 29 this.name = name; 30 } 31 public Student getStudent() { 32 return student; 33 } 34 public void setStudent(Student student) { 35 this.student = student; 36 } 37 public List getList() { 38 return list; 39 } 40 public void setList(List list) { 41 this.list = list; 42 } 43 public Set getSets() { 44 return sets; 45 } 46 public void setSets(Set sets) { 47 this.sets = sets; 48 } 49 public Map getMap() { 50 return map; 51 } 52 public void setMap(Map map) { 53 this.map = map; 54 } 55 public Properties getProperties() { 56 return properties; 57 } 58 public void setProperties(Properties properties) { 59 this.properties = properties; 60 } 61 public Object[] getObjects() { 62 return objects; 63 } 64 public void setObjects(Object[] objects) { 65 this.objects = objects; 66 } 67 68 }
Studnet
package com.spring.di.xml.setter; public class Student { public void say(){ System.out.println("student"); } }
测试代码
package com.spring.di.xml.setter.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.di.xml.setter.Person; public class DIXMLTest { @Test public void testDI_XML_Setter(){ ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml"); Person person=(Person) context.getBean("person"); System.out.println(person.getName()); System.out.println(person.getList().size()); } }
输出:
狗蛋
4
时间: 2024-10-14 23:31:47