设置注入和构造注入
注入分为 设置注入和 构造注入
前面spring3中的例子是设值注入(包括简单类型,引用类型 )
引用类型用ref,简单类型用value。
其他类型的设置注入
array ,List,Map,Set,Properties
package com.cn.service; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import com.cn.dao.DoSomeThingsOracleDao; public class DoSomeThingsService { //删除属性的get,set方法 private int intValue; private String y; private DoSomeThingsOracleDao dao; private int[] arrInt; private List<String> StrList; private Set<Long> setLong; private Map<String, Object> map; private Properties properties; private List<Map<String, Object>> a; public List<Map<String, Object>> getA() { return a; } public void setA(List<Map<String, Object>> a) { this.a = a; } public DoSomeThingsOracleDao getDao() { return dao; } public void setDao(DoSomeThingsOracleDao dao) { this.dao = dao; } public int[] getArrInt() { return arrInt; } public void setArrInt(int[] arrInt) { this.arrInt = arrInt; } public List<String> getStrList() { return StrList; } public void setStrList(List<String> strList) { StrList = strList; } public Set<Long> getSetLong() { return setLong; } public void setSetLong(Set<Long> setLong) { this.setLong = setLong; } public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public void add(){ System.out.println("service do insert"); dao.insert(); } @Override public String toString() { return "DoSomeThingsService [dao=" + dao + ", arrInt=" + Arrays.toString(arrInt) + ", StrList=" + StrList + ", setLong=" + setLong + ", map=" + map + ", properties=" + properties + ", a=" + a + "]"; } } <?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="doSomeDao" class="com.cn.dao.DoSomeThingsOracleDao" /> <bean id="doSomeService" class="com.cn.service.DoSomeThingsService" > <property name="dao" ref="doSomeDao"/> <!-- array --> <property name="arrInt"> <array> <value>5</value> <value>4</value> </array> </property> <!-- list --> <property name="StrList"> <list> <value>test</value> <value>234</value> </list> </property> <!-- set --> <property name="setLong"> <set> <value>4</value> <value>234</value> </set> </property> <!-- map --> <property name="map"> <map> <entry key="gts" value-ref="doSomeDao"/> </map> </property> <!-- properties --> <property name="properties"> <props> <prop key="lq">eee</prop> </props> </property> <property name="a"> <list> <map> <entry key="key1" value-ref="doSomeDao"/> <entry key="key2" value-ref="doSomeDao"/> </map> <map> <entry key="key3" value-ref="doSomeDao"/> <entry key="key4" value-ref="doSomeDao"/> </map> </list> </property> </bean> </beans> DoSomeThingsService [dao=com.cn.dao.DoSomeThingsOracleDao@5a8e6209, arrInt=[5, 4], StrList=[test, 234], setLong=[4, 234], map={gts=com.cn.dao.DoSomeThingsOracleDao@5a8e6209}, properties={lq=eee}, a=[{key1=com.cn.dao.DoSomeThingsOracleDao@5a8e6209, key2=com.cn.dao.DoSomeThingsOracleDao@5a8e6209}, {key3=com.cn.dao.DoSomeThingsOracleDao@5a8e6209, key4=com.cn.dao.DoSomeThingsOracleDao@5a8e6209}]]
array 可以有三种方式。<array>,<list>,value
<!-- array --> <property name="arrInt"> <list> <value>5</value> <value>4</value> </list> </property> <!-- array --> <property name="arrInt" value="5,4,8"/>
构造注入
构造注入就是利用构造方法来进行属性赋值。
设置注入用的是属性的set方法。
package com.cn.service;
import com.cn.dao.DoSomeThingsOracleDao;
public class DoSomeThingsService {
//删除属性的get,set方法
private int intValue;
private String y;
private DoSomeThingsOracleDao dao;
public DoSomeThingsService(int intValue,String y,DoSomeThingsOracleDao dao) {
this.intValue = intValue;
this.y = y;
this.dao = dao;
}
public void add(){
System.out.println("service do insert");
dao.insert();
}
@Override
public String toString() {
return "DoSomeThingsService [intValue=" + intValue + ", y=" + y + ", dao=" + dao + "]";
}
}
<bean id="doSomeDao" class="com.cn.dao.DoSomeThingsOracleDao" />
<bean id="doSomeService" class="com.cn.service.DoSomeThingsService" >
<constructor-arg name="intValue" value="6" />
<constructor-arg name = "y" value ="test"/>
<constructor-arg name = "dao" ref ="doSomeDao"/>
</bean>
public static void main(String[] args) {
String resource = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
DoSomeThingsService service= (DoSomeThingsService) ac.getBean("doSomeService");
System.out.println(service.toString());
}
DoSomeThingsService [intValue=6, y=test, dao=com.cn.dao.DoSomeThingsOracleDao@eec5a4a]
也可以这么写
<bean id="doSomeService" class="com.cn.service.DoSomeThingsService" >
<constructor-arg index="0" value="6" />
<constructor-arg index="1" value ="test"/>
<constructor-arg index="2" ref ="doSomeDao"/>
</bean>
或者
<!-- 省略index 之后,参数的顺序必须和构造函数的参数顺序一致-->
<bean id="doSomeService" class="com.cn.service.DoSomeThingsService" >
<constructor-arg value="6" />
<constructor-arg value ="test"/>
<constructor-arg ref ="doSomeDao"/>
</bean>
自动注入
只有引用类型可以自动注入,由容器spring自动赋值。
有两种方式:
byName:根据引用类型的属性名字自动注入。配置文件中的id和属性名称一样,数据类型也一样,自动注入。
byType:根据类的全限定名称自动注入。java中,引用类型的数据类型和配置文件配置的bean 的class是同源关系,能自动注入。
同源关系:1类型一样;2父类和子类的关系;3接口与实现类的关系
自动注入--byName
package com.cn.service; import com.cn.dao.DoSomeThingsOracleDao; public class DoSomeThingsService { private DoSomeThingsOracleDao dao; public DoSomeThingsOracleDao getDao() { return dao; } public void setDao(DoSomeThingsOracleDao dao) { this.dao = dao; } @Override public String toString() { return "DoSomeThingsService [dao=" + dao + "]"; } } <bean id="dao" class="com.cn.dao.DoSomeThingsOracleDao" /> <bean id="doSomeService" class="com.cn.service.DoSomeThingsService" autowire="byName"> </bean> DoSomeThingsService [dao=com.cn.dao.DoSomeThingsOracleDao@cb644e]
byName 的 dao 的bean id 值必须和 service类中的属性名字一样。并且class是一样的类型
自动注入--byType
符合添加的对象只能有一个
<bean id="oracleDao" class="com.cn.dao.DoSomeThingsOracleDao" /> <bean id="doSomeService" class="com.cn.service.DoSomeThingsService" autowire="byType"> </bean> DoSomeThingsService [dao=com.cn.dao.DoSomeThingsOracleDao@4bec1f0c]
byType对bean 的id 没有要求。
同源关系还需要再测试一下。