1. Spring IoC的依赖注入
1) 使用构造方法来注入依赖:比较麻烦
<constructor-arg index="构造方法参数的索引(从0开始)" value="给这个属性注入的值"/>
2) 使用setter方法来注入依赖:建议使用
<property name="属性名" [value="要注入的值"|ref="引用自Spring容器中的其它JavaBean的ID"]/>
3) 集合类型的注入
<property>
<set>、<list>、<map>、<props>
</property>
首先来看看第1)和第2)的综合示例
JavaBean.java
package com.javacrazyer.bean; public class JavaBean { private int intValue; private double doubleValue; private boolean booleanValue; private char charValue; private String stringValue; public JavaBean(){} public JavaBean(int intValue, double doubleValue, boolean booleanValue, char charValue, String stringValue) { super(); this.intValue = intValue; this.doubleValue = doubleValue; this.booleanValue = booleanValue; this.charValue = charValue; this.stringValue = stringValue; } public int getIntValue() { return intValue; } public void setIntValue(int intValue) { this.intValue = intValue; } public double getDoubleValue() { return doubleValue; } public void setDoubleValue(double doubleValue) { this.doubleValue = doubleValue; } public boolean isBooleanValue() { return booleanValue; } public void setBooleanValue(boolean booleanValue) { this.booleanValue = booleanValue; } public char getCharValue() { return charValue; } public void setCharValue(char charValue) { this.charValue = charValue; } public String getStringValue() { return stringValue; } public void setStringValue(String stringValue) { this.stringValue = stringValue; } }
JavaBean2.java
package com.javacrazyer.bean; public class JavaBean2 { private int intValue; private double doubleValue; private boolean booleanValue; private char charValue; private String stringValue; public JavaBean2(){} public int getIntValue() { return intValue; } public void setIntValue(int intValue) { this.intValue = intValue; } public double getDoubleValue() { return doubleValue; } public void setDoubleValue(double doubleValue) { this.doubleValue = doubleValue; } public boolean isBooleanValue() { return booleanValue; } public void setBooleanValue(boolean booleanValue) { this.booleanValue = booleanValue; } public char getCharValue() { return charValue; } public void setCharValue(char charValue) { this.charValue = charValue; } public String getStringValue() { return stringValue; } public void setStringValue(String stringValue) { this.stringValue = stringValue; } }
JavaBean4.java
package com.javacrazyer.bean; public class JavaBean4 { public void init(){ System.out.println("对JavaBean4进行初始化"); } public void destroy(){ System.out.println("对JavaBean4进行资源回收"); } }
Spring配置applicationContext-base.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: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.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="javaBean" class="com.javacrazyer.bean.JavaBean" scope="prototype">
<constructor-arg index="0" value="123"/>
<constructor-arg index="1" value="456.789"/>
<constructor-arg index="2" value="true"/>
<constructor-arg index="3" value="中"/>
<constructor-arg index="4" value="中国北京"/>
</bean>
<bean id="javaBean2" class="com.javacrazyer.bean.JavaBean2">
<property name="intValue" value="321"/>
<property name="doubleValue" value="876.54"/>
<property name="booleanValue" value="false"/>
<property name="charValue" value="中"/>
<property name="stringValue" value="java"/>
</bean>
<bean id="javaBean4" class="com.javacrazyer.bean.JavaBean4"
init-method="init"
destroy-method="destroy"/>
</beans>
测试示例
package com.javacrazyer.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.javacrazyer.bean.JavaBean;
import com.javacrazyer.bean.JavaBean2;
import com.javacrazyer.bean.JavaBean4;
public class ContstructorDITest {
private static ApplicationContext context;
@BeforeClass
public static void init(){
context = new ClassPathXmlApplicationContext("applicationContext-base.xml");
}
@Test
public void testDI(){
//构造器方式注入测试
JavaBean jb = (JavaBean)context.getBean("javaBean");
System.out.println(jb.getIntValue());
System.out.println(jb.getDoubleValue());
System.out.println(jb.isBooleanValue());
System.out.println(jb.getCharValue());
System.out.println(jb.getStringValue());
//属性方式注入测试
JavaBean2 jb2 = (JavaBean2)context.getBean("javaBean2");
System.out.println(jb2.getIntValue());
System.out.println(jb2.getDoubleValue());
System.out.println(jb2.isBooleanValue());
System.out.println(jb2.getCharValue());
System.out.println(jb2.getStringValue());
}
@Test
public void testScope(){
JavaBean jb = (JavaBean)context.getBean("javaBean");
JavaBean jb2 = (JavaBean)context.getBean("javaBean");
System.out.println(jb == jb2);
}
@Test
public void testInit() throws InterruptedException{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-base.xml");
JavaBean4 jb4 = (JavaBean4)context.getBean("javaBean4");
jb4 = null;
Thread.sleep(10000);
}
}
对于testDI方法主要是构造和属性注入方式的测试方法,测试结果为
123
456.789
true
中
中国北京
321
876.54
false
中
java
对于testScope方法,其实主要是Spring中bean的创建模式,测试结果为
false
指定Spring容器管理的Bean的生存范围
<bean>标记上有一个scope属性,它的可选值有:
sigleton:一个容器只有Bean的一个实例。默认值
prototype: 使用一次就创建一个实例
request:在HTTP请求范围内。只有在使用具有Web能力的Spring容器时才有效
session:HTTP Session范围。同上。
而对于最后一个方法testInit,则是对于bean创建和销毁时调用方法的测试,结果为
对JavaBean4进行初始化
集合类型的注入示例
javaBean3.java
package com.javacrazyer.bean;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class JavaBean3 {
private Set<String> strSet;
private List<String> strList;
private Map<String, String> strMap;
private Properties props;
private JavaBean2 javaBean2;
public Set<String> getStrSet() {
return strSet;
}
public void setStrSet(Set<String> strSet) {
this.strSet = strSet;
}
public List<String> getStrList() {
return strList;
}
public void setStrList(List<String> strList) {
this.strList = strList;
}
public Map<String, String> getStrMap() {
return strMap;
}
public void setStrMap(Map<String, String> strMap) {
this.strMap = strMap;
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
public JavaBean2 getJavaBean2() {
return javaBean2;
}
public void setJavaBean2(JavaBean2 javaBean2) {
this.javaBean2 = javaBean2;
}
}
Spring配置文件applicationContext-collection.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: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.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="javaBean3" class="com.javacrazyer.bean.JavaBean3">
<property name="strSet">
<set>
<value>abc</value>
<value>中国</value>
</set>
</property>
<property name="strList">
<list>
<value>asdfasdf</value>
<value>xxxx</value>
</list>
</property>
<property name="strMap">
<map>
<entry key="cn" value="中国"/>
<entry key="us" value="美国"/>
</map>
</property>
<property name="props">
<props>
<prop key="xxx">XXX</prop>
</props>
</property>
<property name="javaBean2" ref="javaBean2"/>
</bean>
</beans>
测试类
@Test
public void testCollectionDI(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-*.xml");
//上边的-*指的就是applicationContext-base.xml和applicationContext-collection.xml
JavaBean3 jb = (JavaBean3)context.getBean("javaBean3");
System.out.println(jb.getStrSet());
System.out.println(jb.getStrList());
System.out.println(jb.getStrMap());
System.out.println(jb.getProps());
JavaBean2 jb2 = jb.getJavaBean2();
if(jb2 != null){
System.out.println("jb2.intValue" + jb2.getIntValue());
}
}
测试结果
[abc, 中国]
[asdfasdf, xxxx]
{cn=中国, us=美国}
{xxx=XXX}
jb2.intValue321
最后,介绍下继承装配
<bean>元素提供了两个特殊属性来支持装配Bean的继承:
parent:指定父类Bean的id。 相当于java中extends
abstract:如果设置为true,表示此Bean为抽象的,不能被Spring容器实例化。
具体示例
ParentBean.java
package com.javacrazyer.bean;
public class ParentBean {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
ChildBean.java
package com.javacrazyer.bean;
public class ChildBean extends ParentBean {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Spring配置applicationContext-adv.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: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.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="parent" class="com.javacrazyer.bean.ParentBean" abstract="true">
<property name="name" value="javacrazyer"/>
<property name="age" value="38"/>
</bean>
<bean id="child" class="com.javacrazyer.bean.ChildBean" parent="parent">
<property name="address" value="北京大兴"/>
<property name="age" value="16"/>
</bean>
</beans>
测试代码
@Test
public void testExtends(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-*.xml");
ChildBean c = (ChildBean)context.getBean("child");
System.out.println(c.getName());
System.out.println(c.getAge());
System.out.println(c.getAddress());
}
测试结果
javacrazyer
16
北京大兴