IOC的依赖注入方式有:
接口注入(不常用):定义一个注入接口,在需要注入的类中实现此接口,由于这种方法具有侵入性,所以不常用
package com.zhmg.spring;
import java.util.List; import java.util.Map; import java.util.Set; /** * 使用Setters方式进行普通属性的IOC注入 * @author Administrator * */ public class CommonPropertyInjection {
private String strValue;
private int intValue;
private List listValue;
private Set setValue;
private String []arrayStr;
private Map mapValue;
public String getStrValue() { return strValue; }
public void setStrValue(String strValue) { this.strValue = strValue; }
public int getIntValue() { return intValue; }
public void setIntValue(int intValue) { this.intValue = intValue; }
public List getListValue() { return listValue; }
public void setListValue(List listValue) { this.listValue = listValue; }
public Set getSetValue() { return setValue; }
public void setSetValue(Set setValue) { this.setValue = setValue; }
public String[] getArrayStr() { return arrayStr; }
public void setArrayStr(String[] arrayStr) { this.arrayStr = arrayStr; }
public Map getMapValue() { return mapValue; }
public void setMapValue(Map mapValue) { this.mapValue = mapValue; }
}
|
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="commonProperty" class="com.zhmg.spring.CommonPropertyInjection">
<!-- 普通字符串的注入 --> <property name="strValue" value="使用Setters方式进行字符串的IOC注入"/>
<!-- int的注入 --> <!-- 也可以写成如下形式 <property name="intValue" value="123"/> --> <property name="intValue"> <value>123</value> </property>
<!--List的注入 --> <property name="listValue"> <list> <value>list1</value> <value>list2</value> </list> </property>
<!-- Set的注入 --> <property name="setValue"> <set> <value>set1</value> <value>set2</value> </set> </property>
<!-- 字符串数组的注入 --> <property name="arrayStr"> <list> <value>list3</value> <value>list4</value> </list> </property>
<!-- Map的注入 --> <property name="mapValue"> <map> <entry key="k1" value="v1" /> <entry key="k2" value="v2" /> </map> </property>
</bean> </beans> |
package com.zhmg.spring;
import junit.framework.TestCase;
import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InjectionTest extends TestCase {
BeanFactory factory; protected void setUp() throws Exception { factory = new ClassPathXmlApplicationContext("applicationContext.xml"); }
public void testInjection(){ CommonPropertyInjection commonProperty = (CommonPropertyInjection)factory.getBean("commonProperty");
System.out.println("strValue=" + commonProperty.getStrValue()); System.out.println("intValue=" + commonProperty.getIntValue()); System.out.println("listValue=" + commonProperty.getListValue()); System.out.println("setValue=" + commonProperty.getSetValue()); System.out.println("arrayStr=" + commonProperty.getArrayStr()); System.out.println("mapValue=" + commonProperty.getMapValue()); }
} |
测试结果: strValue=使用Setters方式进行字符串的IOC注入 intValue=123 listValue=[list1, list2] setValue=[set1, set2] arrayStr=[Ljava.lang.String;@c837cd mapValue={k1=v1, k2=v2} |
--------------此实例设计思想以及代码大部分来自北京尚学堂