(二)spring 之IOC容器

来源:互联网 发布:网络小票机怎么连接 编辑:程序博客网 时间:2024/05/22 08:29

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

Java代码  收藏代码
  1. <span style="font-size: medium;">package com.javacrazyer.bean;  
  2.   
  3.   
  4. public class JavaBean {  
  5.       
  6.     private int intValue;  
  7.     private double doubleValue;  
  8.     private boolean booleanValue;  
  9.     private char charValue;  
  10.     private String stringValue;  
  11.       
  12.     public JavaBean(){}  
  13.   
  14.     public JavaBean(int intValue, double doubleValue, boolean booleanValue,  
  15.             char charValue, String stringValue) {  
  16.         super();  
  17.         this.intValue = intValue;  
  18.         this.doubleValue = doubleValue;  
  19.         this.booleanValue = booleanValue;  
  20.         this.charValue = charValue;  
  21.         this.stringValue = stringValue;  
  22.     }  
  23.   
  24.     public int getIntValue() {  
  25.         return intValue;  
  26.     }  
  27.   
  28.     public void setIntValue(int intValue) {  
  29.         this.intValue = intValue;  
  30.     }  
  31.   
  32.     public double getDoubleValue() {  
  33.         return doubleValue;  
  34.     }  
  35.   
  36.     public void setDoubleValue(double doubleValue) {  
  37.         this.doubleValue = doubleValue;  
  38.     }  
  39.   
  40.     public boolean isBooleanValue() {  
  41.         return booleanValue;  
  42.     }  
  43.   
  44.     public void setBooleanValue(boolean booleanValue) {  
  45.         this.booleanValue = booleanValue;  
  46.     }  
  47.   
  48.     public char getCharValue() {  
  49.         return charValue;  
  50.     }  
  51.   
  52.     public void setCharValue(char charValue) {  
  53.         this.charValue = charValue;  
  54.     }  
  55.   
  56.     public String getStringValue() {  
  57.         return stringValue;  
  58.     }  
  59.   
  60.     public void setStringValue(String stringValue) {  
  61.         this.stringValue = stringValue;  
  62.     }  
  63.       
  64. }</span><span style="font-size: medium;">  
  65. </span>  

 JavaBean2.java

Java代码  收藏代码
  1. <span style="font-size: medium;">package com.javacrazyer.bean;  
  2.   
  3.   
  4. public class JavaBean2 {  
  5.       
  6.     private int intValue;  
  7.     private double doubleValue;  
  8.     private boolean booleanValue;  
  9.     private char charValue;  
  10.     private String stringValue;  
  11.       
  12.     public JavaBean2(){}  
  13.   
  14.   
  15.     public int getIntValue() {  
  16.         return intValue;  
  17.     }  
  18.   
  19.     public void setIntValue(int intValue) {  
  20.         this.intValue = intValue;  
  21.     }  
  22.   
  23.     public double getDoubleValue() {  
  24.         return doubleValue;  
  25.     }  
  26.   
  27.     public void setDoubleValue(double doubleValue) {  
  28.         this.doubleValue = doubleValue;  
  29.     }  
  30.   
  31.     public boolean isBooleanValue() {  
  32.         return booleanValue;  
  33.     }  
  34.   
  35.     public void setBooleanValue(boolean booleanValue) {  
  36.         this.booleanValue = booleanValue;  
  37.     }  
  38.   
  39.     public char getCharValue() {  
  40.         return charValue;  
  41.     }  
  42.   
  43.     public void setCharValue(char charValue) {  
  44.         this.charValue = charValue;  
  45.     }  
  46.   
  47.     public String getStringValue() {  
  48.         return stringValue;  
  49.     }  
  50.   
  51.     public void setStringValue(String stringValue) {  
  52.         this.stringValue = stringValue;  
  53.     }  
  54.       
  55.       
  56.       
  57. }</span><span style="font-size: medium;">  
  58. </span>  

 JavaBean4.java

Java代码  收藏代码
  1. <span style="font-size: medium;">package com.javacrazyer.bean;  
  2.   
  3. public class JavaBean4 {  
  4.       
  5.     public void init(){  
  6.         System.out.println("对JavaBean4进行初始化");  
  7.     }  
  8.       
  9.     public void destroy(){  
  10.         System.out.println("对JavaBean4进行资源回收");  
  11.     }  
  12.       
  13. }</span><span style="font-size: medium;">  
  14. </span>  

 Spring配置applicationContext-base.xml

Xml代码  收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.         xmlns:aop="http://www.springframework.org/schema/aop"  
  5.         xmlns:tx="http://www.springframework.org/schema/tx"  
  6.         xsi:schemaLocation="  
  7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  9.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  10.   
  11.     <bean id="javaBean" class="com.javacrazyer.bean.JavaBean" scope="prototype">  
  12.         <constructor-arg index="0" value="123"/>  
  13.         <constructor-arg index="1" value="456.789"/>  
  14.         <constructor-arg index="2" value="true"/>  
  15.         <constructor-arg index="3" value="中"/>  
  16.         <constructor-arg index="4" value="中国北京"/>  
  17.     </bean>  
  18.       
  19.     <bean id="javaBean2" class="com.javacrazyer.bean.JavaBean2">  
  20.         <property name="intValue" value="321"/>  
  21.         <property name="doubleValue" value="876.54"/>  
  22.         <property name="booleanValue" value="false"/>  
  23.         <property name="charValue" value="中"/>  
  24.         <property name="stringValue" value="java"/>  
  25.     </bean>  
  26.       
  27.     <bean id="javaBean4" class="com.javacrazyer.bean.JavaBean4"   
  28.             init-method="init"   
  29.             destroy-method="destroy"/>  
  30. </beans>  
  31. </span>  

 

 测试示例

Java代码  收藏代码
  1. <span style="font-size: medium;">package com.javacrazyer.test;  
  2.   
  3. import org.junit.BeforeClass;  
  4. import org.junit.Test;  
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. import com.javacrazyer.bean.JavaBean;  
  9. import com.javacrazyer.bean.JavaBean2;  
  10. import com.javacrazyer.bean.JavaBean4;  
  11.   
  12. public class ContstructorDITest {  
  13.     private static ApplicationContext context;  
  14.       
  15.       
  16.     @BeforeClass  
  17.     public static void init(){  
  18.         context = new ClassPathXmlApplicationContext("applicationContext-base.xml");  
  19.     }  
  20.       
  21.     @Test  
  22.     public void testDI(){  
  23.           
  24.         //构造器方式注入测试  
  25.         JavaBean jb = (JavaBean)context.getBean("javaBean");  
  26.         System.out.println(jb.getIntValue());  
  27.         System.out.println(jb.getDoubleValue());  
  28.         System.out.println(jb.isBooleanValue());  
  29.         System.out.println(jb.getCharValue());  
  30.         System.out.println(jb.getStringValue());  
  31.         //属性方式注入测试  
  32.         JavaBean2 jb2 = (JavaBean2)context.getBean("javaBean2");  
  33.         System.out.println(jb2.getIntValue());  
  34.         System.out.println(jb2.getDoubleValue());  
  35.         System.out.println(jb2.isBooleanValue());  
  36.         System.out.println(jb2.getCharValue());  
  37.         System.out.println(jb2.getStringValue());  
  38.     }  
  39.       
  40.     @Test  
  41.     public void testScope(){  
  42.         JavaBean jb = (JavaBean)context.getBean("javaBean");  
  43.         JavaBean jb2 = (JavaBean)context.getBean("javaBean");  
  44.         System.out.println(jb == jb2);  
  45.     }  
  46.       
  47.     @Test  
  48.     public void testInit() throws InterruptedException{  
  49.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-base.xml");  
  50.         JavaBean4 jb4 = (JavaBean4)context.getBean("javaBean4");  
  51.         jb4 = null;  
  52.         Thread.sleep(10000);  
  53.           
  54.     }  
  55. }  
  56. </span>  

 

对于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

Java代码  收藏代码
  1. <span style="font-size: medium;">package com.javacrazyer.bean;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5. import java.util.Properties;  
  6. import java.util.Set;  
  7.   
  8.   
  9. public class JavaBean3 {  
  10.       
  11.     private Set<String> strSet;  
  12.     private List<String> strList;  
  13.     private Map<String, String> strMap;  
  14.     private Properties props;  
  15.     private JavaBean2 javaBean2;  
  16.       
  17.       
  18.     public Set<String> getStrSet() {  
  19.         return strSet;  
  20.     }  
  21.     public void setStrSet(Set<String> strSet) {  
  22.         this.strSet = strSet;  
  23.     }  
  24.     public List<String> getStrList() {  
  25.         return strList;  
  26.     }  
  27.     public void setStrList(List<String> strList) {  
  28.         this.strList = strList;  
  29.     }  
  30.     public Map<String, String> getStrMap() {  
  31.         return strMap;  
  32.     }  
  33.     public void setStrMap(Map<String, String> strMap) {  
  34.         this.strMap = strMap;  
  35.     }  
  36.     public Properties getProps() {  
  37.         return props;  
  38.     }  
  39.     public void setProps(Properties props) {  
  40.         this.props = props;  
  41.     }  
  42.     public JavaBean2 getJavaBean2() {  
  43.         return javaBean2;  
  44.     }  
  45.     public void setJavaBean2(JavaBean2 javaBean2) {  
  46.         this.javaBean2 = javaBean2;  
  47.     }  
  48.       
  49.       
  50. }  
  51. </span>  

 

Spring配置文件applicationContext-collection.xml

Xml代码  收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.         xmlns:aop="http://www.springframework.org/schema/aop"  
  5.         xmlns:tx="http://www.springframework.org/schema/tx"  
  6.         xsi:schemaLocation="  
  7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  9.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  10.   
  11.     <bean id="javaBean3" class="com.javacrazyer.bean.JavaBean3">  
  12.         <property name="strSet">  
  13.             <set>  
  14.                 <value>abc</value>  
  15.                 <value>中国</value>  
  16.             </set>  
  17.         </property>  
  18.           
  19.         <property name="strList">  
  20.             <list>  
  21.                 <value>asdfasdf</value>  
  22.                 <value>xxxx</value>  
  23.             </list>  
  24.         </property>  
  25.           
  26.         <property name="strMap">  
  27.             <map>  
  28.                 <entry key="cn" value="中国"/>  
  29.                 <entry key="us" value="美国"/>  
  30.             </map>  
  31.         </property>  
  32.           
  33.         <property name="props">  
  34.             <props>  
  35.                 <prop key="xxx">XXX</prop>  
  36.             </props>  
  37.         </property>  
  38.           
  39.         <property name="javaBean2" ref="javaBean2"/>  
  40.     </bean>  
  41.       
  42. </beans>  
  43. </span>  

 

测试类

Java代码  收藏代码
  1. <span style="font-size: medium;">   @Test  
  2.     public void testCollectionDI(){  
  3.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-*.xml");  
  4.         //上边的-*指的就是applicationContext-base.xml和applicationContext-collection.xml  
  5.         JavaBean3 jb = (JavaBean3)context.getBean("javaBean3");  
  6.           
  7.         System.out.println(jb.getStrSet());  
  8.         System.out.println(jb.getStrList());  
  9.         System.out.println(jb.getStrMap());  
  10.         System.out.println(jb.getProps());  
  11.           
  12.         JavaBean2 jb2 = jb.getJavaBean2();  
  13.         if(jb2 != null){  
  14.             System.out.println("jb2.intValue" + jb2.getIntValue());  
  15.         }  
  16.     }  
  17.     </span>  

 

测试结果

[abc, 中国]
[asdfasdf, xxxx]
{cn=中国, us=美国}
{xxx=XXX}
jb2.intValue321

 

最后,介绍下继承装配

<bean>元素提供了两个特殊属性来支持装配Bean的继承:
parent:指定父类Bean的id。 相当于java中extends
abstract:如果设置为true,表示此Bean为抽象的,不能被Spring容器实例化。

具体示例

ParentBean.java

Java代码  收藏代码
  1. <span style="font-size: medium;">package com.javacrazyer.bean;  
  2.   
  3. public class ParentBean {  
  4.       
  5.     private String name;  
  6.       
  7.     private int age;  
  8.   
  9.     public String getName() {  
  10.         return name;  
  11.     }  
  12.   
  13.     public void setName(String name) {  
  14.         this.name = name;  
  15.     }  
  16.   
  17.     public int getAge() {  
  18.         return age;  
  19.     }  
  20.   
  21.     public void setAge(int age) {  
  22.         this.age = age;  
  23.     }  
  24. }  
  25. </span>  

 

 ChildBean.java

Java代码  收藏代码
  1. <span style="font-size: medium;">package com.javacrazyer.bean;  
  2.   
  3. public class ChildBean extends ParentBean {  
  4.   
  5.     private String address;  
  6.   
  7.     public String getAddress() {  
  8.         return address;  
  9.     }  
  10.   
  11.     public void setAddress(String address) {  
  12.         this.address = address;  
  13.     }  
  14. }  
  15. </span>  

 

 Spring配置applicationContext-adv.xml

Xml代码  收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.         xmlns:aop="http://www.springframework.org/schema/aop"  
  5.         xmlns:tx="http://www.springframework.org/schema/tx"  
  6.         xsi:schemaLocation="  
  7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  9.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  10.   
  11.     <bean id="parent" class="com.javacrazyer.bean.ParentBean" abstract="true">  
  12.         <property name="name" value="javacrazyer"/>  
  13.         <property name="age" value="38"/>  
  14.     </bean>  
  15.       
  16.     <bean id="child" class="com.javacrazyer.bean.ChildBean" parent="parent">  
  17.         <property name="address" value="北京大兴"/>  
  18.         <property name="age" value="16"/>  
  19.     </bean>  
  20.       
  21. </beans>  
  22. </span>  

 

 测试代码

Java代码  收藏代码
  1. <span style="font-size: medium;">   @Test  
  2.     public void testExtends(){  
  3.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-*.xml");  
  4.         ChildBean c = (ChildBean)context.getBean("child");  
  5.         System.out.println(c.getName());  
  6.         System.out.println(c.getAge());  
  7.         System.out.println(c.getAddress());  
  8.     }</span>  

 测试结果

javacrazyer
16
北京大兴
原创粉丝点击