spring di,即依賴註入,從應用的淺顯意義來講就是對屬性賦值 1.用setter賦值,在spring的applicationContext.xml配置文件的bean下的property標簽 屬性name指定屬性名,屬性value指定值,一般用於基本數據 類型的包裝類型 屬性ref指定值,一般用 ...
spring di,即依賴註入,從應用的淺顯意義來講就是對屬性賦值
1.用setter賦值,在spring的applicationContext.xml配置文件的bean下的property標簽
屬性name指定屬性名,屬性value指定值,一般用於基本數據 類型的包裝類型
屬性ref指定值,一般用於引用類型,還有list標簽,下麵value標簽,
set標簽下麵value標簽,map標簽下麵entry,下麵分別有key,value,
還有props,下麵prop,key
public class Student { public void say(){ System.out.println("student"); } } public class Person { private Long pid;//包裝類型 private String pname;//String類型 private Student student;//引用類型 private List lists; private Set sets; public Long getPid() { return pid; } public void setPid(Long pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public List getLists() { return lists; } public void setLists(List lists) { this.lists = lists; } public Set getSets() { return sets; } public void setSets(Set sets) { this.sets = sets; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } private Map map; private Properties 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-2.5.xsd"> <bean id="person" class="cn.di.xml.set.Person"> <!-- property就是代表屬性 在spring中基本類型(包裝類型和String)都可以用value來賦值 引用類型用ref賦值 --> <property name="pid" value="5"></property> <property name="pname" value="王五"></property> <property name="student"> <ref bean="student"/> </property> <property name="lists"> <list> <value>list1</value> <value>list2</value> <ref bean="student"/> </list> </property> <property name="sets"> <set> <value>set1</value> <value>set2</value> <ref bean="student"/> </set> </property> <property name="map"> <map> <entry key="map1"> <value>map1</value> </entry> <entry key="map2"> <value>map2</value> </entry> <entry key="map3"> <ref bean="student"/> </entry> </map> </property> <property name="properties"> <props> <prop key="prop1"> prop1 </prop> </props> </property> </bean> <bean id="student" class="cn.di.xml.set.Student"></bean> </beans>
@Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person)context.getBean("person"); person.getStudent().say(); System.out.println(person.getPid()); System.out.println(person.getPname()); List lists = person.getLists(); for(int i=0;i<lists.size();i++){ System.out.println(lists.get(i).toString()); } }
2.利用構造函數賦值
在bean下有constructor-arg標簽,裡面有index,type,ref,value屬性
public class Person { private Long pid; public Long getPid() { return pid; } public String getPname() { return pname; } public Student getStudent() { return student; } private String pname; private Student student; public Person(Long pid,String pname){ this.pid = pid; this.pname = pname; } public Person(String pname,Student student){ this.pname = pname; this.student = student; } }
<?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="cn.di.xml.constructor.Person"> <!-- 構造函數的參數 index 第幾個參數,下標從0開始 type 參數的類型 ref 如果類型是引用類型,賦值 value 如果類型是基本類型,賦值 說明: 只能指定一個構造函數 --> <constructor-arg index="0" type="java.lang.String" value="王五"></constructor-arg> <constructor-arg index="1" ref="student"></constructor-arg> </bean> <bean id="student" class="cn.di.xml.constructor.Student"></bean> </beans>