老兄,快来一起过春天!(二)

依赖注入的注入类型可以注入基本类型、集合、类、甚至程序要使用的资源。
下面的例子可以看到applicationContext.xml中注入的各种类型。

在上一篇已经构造好的目录上增加一个util包,新建类 TestOK 。


TestOK 代码如下:
  1. package com.testspring.util;
  2. public class TestOK {
  3.     
  4.     public String testsay(){
  5.         return "你正在调用TestClass.testsay()方法";
  6.     }
  7. }
在这个类只拥有一个测试方法。

然后打开 TestSpringImpl.java 类,在类声明下添加字段、方法。完成后代码如下:

  1. package com.testspring.dao.impl;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Properties;
  6. import java.util.Set;
  7. import org.springframework.core.io.Resource;
  8. import com.testspring.dao.ITestSpring;
  9. import com.testspring.util.TestOK;
  10. public class TestSpringImpl implements ITestSpring {
  11.     
  12.     public List<String> getHello() {
  13.         // TODO Auto-generated method stub
  14.         return null;
  15.     }
  16.     public void setHello(List<String> hello) {
  17.         // TODO Auto-generated method stub
  18.         
  19.     }
  20.     private String teststr;;
  21.     private boolean testbool;
  22.     private int testint;
  23.     private String testnull;
  24.     private TestOK testtc;
  25.     private int[] testintarr;
  26.     private List testlist;  
  27.     private Map testmap;
  28.     private Set testset;
  29.     private Properties prop;
  30.     public TestSpringImpl(){        
  31.     }
  32.     public String say(){
  33.         StringBuffer str = new StringBuffer("");
  34.         
  35.         try{            
  36.             //teststr
  37.             str.append("teststr:" + this.getTeststr() + "/n/n");
  38.             
  39.             //testbool
  40.             str.append("testbool:" + this.isTestbool() + "/n/n");
  41.             
  42.             //testint
  43.             str.append("testint:" + this.getTestint() + "/n/n");
  44.             
  45.             //testnull
  46.             if(null == this.getTestnull())          
  47.                 str.append("testnull:为空/n/n");
  48.             else
  49.                 str.append("testnull:不为空/n/n");
  50.             
  51.             //testtc
  52.             str.append("testtc:" + this.getTesttc().testsay() + "/n/n");
  53.             
  54.             //testintarr
  55.             str.append("testintarr:/n");
  56.             for (int i : testintarr) {
  57.                 str.append("  " + i);
  58.             }
  59.             str.append("/n/n");     
  60.             
  61.             //testlist
  62.             int for_i = 1;
  63.             str.append("testlist:/n");
  64.             for (Object temp : testlist) {
  65.                 str = this.appendStr(for_i, temp, str);
  66.                 for_i ++ ;
  67.             }
  68.             for_i = 1;
  69.             str.append("/n");
  70.             
  71.             //testmap
  72.             str.append("testmap:");
  73.             for(int i = 1 ; i <= testmap.size(); i++) {
  74.                 Object o = testmap.get(""+i);
  75.                 str = this.appendStr(i, o , str);
  76.             }
  77.             str.append("/n/n");
  78.             
  79.             //testset
  80.             str.append("testset:");     
  81.             for (Object o : testset) {
  82.                 str = this.appendStr(for_i, o , str);
  83.                 for_i ++ ;
  84.             }       
  85.             for_i = 1;
  86.             str.append("/n/n");
  87.             
  88.             //prop
  89.             str.append("prop:");
  90.             for(int i = 1 ; i <= prop.size(); i++) {
  91.                 Object o = prop.get(i+"");
  92.                 str = this.appendStr(i, o , str);
  93.             }
  94.             for_i = 1;
  95.             str.append("/n/n");
  96.         }catch(Exception es){
  97.             str.append("/n/n出错了");
  98.             es.printStackTrace();
  99.         }
  100.         
  101.         return "Spring return :/n/n" + str.toString();
  102.     }
  103.     
  104.     public StringBuffer appendStr(int for_i, Object temp,StringBuffer str) throws Exception{
  105.         try{
  106.             switch (for_i) {
  107.             case 1:
  108.                 str.append(for_i + ":    属性类型  " + Double.valueOf(temp.toString()).getClass().getName()
  109.                         + "      属性值  " +temp.toString() + "/n");
  110.                 break;
  111.             case 2:
  112.                 str.append(for_i + ":    属性类型  " +  Boolean.valueOf(temp.toString()).getClass().getName() 
  113.                         + "      属性值  " +temp.toString() + "/n");
  114.                 break;
  115.             case 3:
  116.                 str.append(for_i + ":    属性类型  " + (temp.toString()).getClass().getName() 
  117.                         + "      属性值  " +temp.toString() + "/n");
  118.                 break;
  119.             case 4:
  120.                 str.append(for_i + ":    属性类型  " + Integer.valueOf(temp.toString()).getClass().getName() 
  121.                         + "      属性值  " +temp.toString() + "/n");
  122.                 break;
  123.             case 5:
  124.                 try{
  125.                     str.append(for_i + ":    属性类型  " + ((TestOK)temp).getClass().getName()  
  126.                     + "      属性值  " + ((TestOK)temp).testsay() + "/n");
  127.                 }catch(Exception e){
  128.                     str.append(for_i + ":    属性类型  " + temp.toString().getClass().getName()  
  129.                     + "      属性值  " + temp.toString() + "/n");
  130.                 }
  131.                 
  132.                 break;
  133.             default:
  134.                 break;
  135.             }
  136.         }catch(Exception es)
  137.         {
  138.             throw es;
  139.         }
  140.             return str; 
  141.     }
  142.     public int getTestint() {
  143.         return testint;
  144.     }
  145.     public void setTestint(int testint) {
  146.         this.testint = testint;
  147.     }
  148.     public int[] getTestintarr() {
  149.         return testintarr;
  150.     }
  151.     public void setTestintarr(int[] testintarr) {
  152.         this.testintarr = testintarr;
  153.     }
  154.     public String getTestnull() {
  155.         return testnull;
  156.     }
  157.     public void setTestnull(String testnull) {
  158.         this.testnull = testnull;
  159.     }
  160.     public String getTeststr() {
  161.         return teststr;
  162.     }
  163.     public void setTeststr(String teststr) {
  164.         this.teststr = teststr;
  165.     }
  166.     public boolean isTestbool() {
  167.         return testbool;
  168.     }
  169.     public void setTestbool(boolean testbool) {
  170.         this.testbool = testbool;
  171.     }
  172.     public TestOK getTesttc() {
  173.         return testtc;
  174.     }
  175.     public void setTesttc(TestOK testtc) {
  176.         this.testtc = testtc;
  177.     }
  178.     public Map getTestmap() {
  179.         return testmap;
  180.     }
  181.     public void setTestmap(Map testmap) {
  182.         this.testmap = testmap;
  183.     }
  184.     public Set getTestset() {
  185.         return testset;
  186.     }
  187.     public void setTestset(Set testset) {
  188.         this.testset = testset;
  189.     }
  190.     public Properties getProp() {
  191.         return prop;
  192.     }
  193.     public void setProp(Properties prop) {
  194.         this.prop = prop;
  195.     }
  196.     public List getTestlist() {
  197.         return testlist;
  198.     }
  199.     public void setTestlist(List testlist) {
  200.         this.testlist = testlist;
  201.     }
  202. }
因为 TestSpringImpl.java 添加了多个属性,Spring 为了管理TestSpringImpl,相应的也需要添加多出来的属性注入,否则在spring初始化TestSpringImpl类并不会失败,没有被注入值的属性,将会是null,如果这时去调用null的属性,将会抛出 NullPointerException 空指针错误。因此,可以在applicationContext.xml 中改动TestSpringImpl声明:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3.     xmlns="http://www.springframework.org/schema/beans"
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  6.     <bean id="testok" class="com.testspring.util.TestOK"></bean>
  7.     <bean id="hello" class="com.testspring.dao.impl.TestSpringImpl">
  8.         <property name="teststr" value="成功"></property>
  9.         <property name="testbool" value="true"></property>
  10.         <property name="testint" value="888"></property>
  11.         <property name="testnull"> <null></null> </property>
  12.         <property name="testtc">
  13.             <ref bean="testok" />
  14.         </property>
  15.         <property name="testintarr">
  16.             <list>
  17.                 <value>100</value>
  18.                 <value>200</value>
  19.                 <value>300</value>
  20.                 <value>400</value>
  21.                 <value>500</value>
  22.             </list>
  23.         </property>
  24.         <property name="testlist">
  25.             <list>
  26.                 <value>1.1111</value>
  27.                 <value>true</value>
  28.                 <value>string</value>
  29.                 <value>100</value>
  30.                 <ref bean="testok" />
  31.             </list>
  32.         </property>
  33.         <property name="testmap">
  34.             <map>
  35.                 <entry key="1">
  36.                     <value>1.1111</value>
  37.                 </entry>
  38.                 <entry key="2">
  39.                     <value>true</value>
  40.                 </entry>
  41.                 <entry key="3">
  42.                     <value>string</value>
  43.                 </entry>
  44.                 <entry key="4">
  45.                     <value>100</value>
  46.                 </entry>
  47.                 <entry key="5">
  48.                     <ref bean="testok" />
  49.                 </entry>
  50.             </map>
  51.         </property>
  52.         <property name="testset">
  53.             <set>
  54.                 <value>1.1111</value>
  55.                 <value>true</value>
  56.                 <value>string</value>
  57.                 <value>100</value>
  58.                 <ref bean="testok" />
  59.             </set>
  60.         </property>
  61.         <property name="prop">
  62.             <props>
  63.                 <prop key="1">1.1111</prop>
  64.                 <prop key="2">true</prop>
  65.                 <prop key="3">string</prop>
  66.                 <prop key="4">100</prop>
  67.                 <prop key="5">testok</prop>
  68.             </props>
  69.         </property>
  70.     </bean>
  71. </beans>
这样的话,TestSpringImpl.java的字段将会在初始化完成以后拥有在xml中默认的值。当然你也可以在运行文件时通过setXXX方法去更改某个属性。打开上一篇的类 HelloWord.java,作为java Application运行,可以看到控制台显示如下:

Spring return :

teststr:成功

testbool:true

testint:888

testnull:为空

testtc:你正在调用TestClass.testsay()方法

testintarr:
  100  200  300  400  500

testlist:
1:    属性类型  java.lang.Double      属性值  1.1111
2:    属性类型  java.lang.Boolean      属性值  true
3:    属性类型  java.lang.String      属性值  string
4:    属性类型  java.lang.Integer      属性值  100
5:    属性类型  com.testspring.util.TestOK      属性值  你正在调用TestClass.testsay()方法

testmap:1:    属性类型  java.lang.Double      属性值  1.1111
2:    属性类型  java.lang.Boolean      属性值  true
3:    属性类型  java.lang.String      属性值  string
4:    属性类型  java.lang.Integer      属性值  100
5:    属性类型  com.testspring.util.TestOK      属性值  你正在调用TestClass.testsay()方法


testset:1:    属性类型  java.lang.Double      属性值  1.1111
2:    属性类型  java.lang.Boolean      属性值  true
3:    属性类型  java.lang.String      属性值  string
4:    属性类型  java.lang.Integer      属性值  100
5:    属性类型  com.testspring.util.TestOK      属性值  你正在调用TestClass.testsay()方法


prop:1:    属性类型  java.lang.Double      属性值  1.1111
2:    属性类型  java.lang.Boolean      属性值  true
3:    属性类型  java.lang.String      属性值  string
4:    属性类型  java.lang.Integer      属性值  100
5:    属性类型  java.lang.String      属性值  testok


由此知道,spring的依赖注入可以是string、int、boolean等基本类型,可以是数组、list/map集合、类,甚至可以是应用程序中使用到的资源文件。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值