依赖注入的注入类型可以注入基本类型、集合、类、甚至程序要使用的资源。
在这个类只拥有一个测试方法。
因为 TestSpringImpl.java 添加了多个属性,Spring 为了管理TestSpringImpl,相应的也需要添加多出来的属性注入,否则在spring初始化TestSpringImpl类并不会失败,没有被注入值的属性,将会是null,如果这时去调用null的属性,将会抛出 NullPointerException 空指针错误。因此,可以在applicationContext.xml 中改动TestSpringImpl声明:
这样的话,TestSpringImpl.java的字段将会在初始化完成以后拥有在xml中默认的值。当然你也可以在运行文件时通过setXXX方法去更改某个属性。打开上一篇的类 HelloWord.java,作为java Application运行,可以看到控制台显示如下:
下面的例子可以看到applicationContext.xml中注入的各种类型。
在上一篇已经构造好的目录上增加一个util包,新建类 TestOK 。
![](https://p-blog.csdn.net/images/p_blog_csdn_net/Begin_Game/EntryImages/20080906/spring1_2.jpg.jpg)
TestOK 代码如下:
- package com.testspring.util;
- public class TestOK {
- public String testsay(){
- return "你正在调用TestClass.testsay()方法";
- }
- }
然后打开 TestSpringImpl.java 类,在类声明下添加字段、方法。完成后代码如下:
- package com.testspring.dao.impl;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Properties;
- import java.util.Set;
- import org.springframework.core.io.Resource;
- import com.testspring.dao.ITestSpring;
- import com.testspring.util.TestOK;
- public class TestSpringImpl implements ITestSpring {
- public List<String> getHello() {
- // TODO Auto-generated method stub
- return null;
- }
- public void setHello(List<String> hello) {
- // TODO Auto-generated method stub
- }
- private String teststr;;
- private boolean testbool;
- private int testint;
- private String testnull;
- private TestOK testtc;
- private int[] testintarr;
- private List testlist;
- private Map testmap;
- private Set testset;
- private Properties prop;
- public TestSpringImpl(){
- }
- public String say(){
- StringBuffer str = new StringBuffer("");
- try{
- //teststr
- str.append("teststr:" + this.getTeststr() + "/n/n");
- //testbool
- str.append("testbool:" + this.isTestbool() + "/n/n");
- //testint
- str.append("testint:" + this.getTestint() + "/n/n");
- //testnull
- if(null == this.getTestnull())
- str.append("testnull:为空/n/n");
- else
- str.append("testnull:不为空/n/n");
- //testtc
- str.append("testtc:" + this.getTesttc().testsay() + "/n/n");
- //testintarr
- str.append("testintarr:/n");
- for (int i : testintarr) {
- str.append(" " + i);
- }
- str.append("/n/n");
- //testlist
- int for_i = 1;
- str.append("testlist:/n");
- for (Object temp : testlist) {
- str = this.appendStr(for_i, temp, str);
- for_i ++ ;
- }
- for_i = 1;
- str.append("/n");
- //testmap
- str.append("testmap:");
- for(int i = 1 ; i <= testmap.size(); i++) {
- Object o = testmap.get(""+i);
- str = this.appendStr(i, o , str);
- }
- str.append("/n/n");
- //testset
- str.append("testset:");
- for (Object o : testset) {
- str = this.appendStr(for_i, o , str);
- for_i ++ ;
- }
- for_i = 1;
- str.append("/n/n");
- //prop
- str.append("prop:");
- for(int i = 1 ; i <= prop.size(); i++) {
- Object o = prop.get(i+"");
- str = this.appendStr(i, o , str);
- }
- for_i = 1;
- str.append("/n/n");
- }catch(Exception es){
- str.append("/n/n出错了");
- es.printStackTrace();
- }
- return "Spring return :/n/n" + str.toString();
- }
- public StringBuffer appendStr(int for_i, Object temp,StringBuffer str) throws Exception{
- try{
- switch (for_i) {
- case 1:
- str.append(for_i + ": 属性类型 " + Double.valueOf(temp.toString()).getClass().getName()
- + " 属性值 " +temp.toString() + "/n");
- break;
- case 2:
- str.append(for_i + ": 属性类型 " + Boolean.valueOf(temp.toString()).getClass().getName()
- + " 属性值 " +temp.toString() + "/n");
- break;
- case 3:
- str.append(for_i + ": 属性类型 " + (temp.toString()).getClass().getName()
- + " 属性值 " +temp.toString() + "/n");
- break;
- case 4:
- str.append(for_i + ": 属性类型 " + Integer.valueOf(temp.toString()).getClass().getName()
- + " 属性值 " +temp.toString() + "/n");
- break;
- case 5:
- try{
- str.append(for_i + ": 属性类型 " + ((TestOK)temp).getClass().getName()
- + " 属性值 " + ((TestOK)temp).testsay() + "/n");
- }catch(Exception e){
- str.append(for_i + ": 属性类型 " + temp.toString().getClass().getName()
- + " 属性值 " + temp.toString() + "/n");
- }
- break;
- default:
- break;
- }
- }catch(Exception es)
- {
- throw es;
- }
- return str;
- }
- public int getTestint() {
- return testint;
- }
- public void setTestint(int testint) {
- this.testint = testint;
- }
- public int[] getTestintarr() {
- return testintarr;
- }
- public void setTestintarr(int[] testintarr) {
- this.testintarr = testintarr;
- }
- public String getTestnull() {
- return testnull;
- }
- public void setTestnull(String testnull) {
- this.testnull = testnull;
- }
- public String getTeststr() {
- return teststr;
- }
- public void setTeststr(String teststr) {
- this.teststr = teststr;
- }
- public boolean isTestbool() {
- return testbool;
- }
- public void setTestbool(boolean testbool) {
- this.testbool = testbool;
- }
- public TestOK getTesttc() {
- return testtc;
- }
- public void setTesttc(TestOK testtc) {
- this.testtc = testtc;
- }
- public Map getTestmap() {
- return testmap;
- }
- public void setTestmap(Map testmap) {
- this.testmap = testmap;
- }
- public Set getTestset() {
- return testset;
- }
- public void setTestset(Set testset) {
- this.testset = testset;
- }
- public Properties getProp() {
- return prop;
- }
- public void setProp(Properties prop) {
- this.prop = prop;
- }
- public List getTestlist() {
- return testlist;
- }
- public void setTestlist(List testlist) {
- this.testlist = testlist;
- }
- }
- <?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-2.0.xsd">
- <bean id="testok" class="com.testspring.util.TestOK"></bean>
- <bean id="hello" class="com.testspring.dao.impl.TestSpringImpl">
- <property name="teststr" value="成功"></property>
- <property name="testbool" value="true"></property>
- <property name="testint" value="888"></property>
- <property name="testnull"> <null></null> </property>
- <property name="testtc">
- <ref bean="testok" />
- </property>
- <property name="testintarr">
- <list>
- <value>100</value>
- <value>200</value>
- <value>300</value>
- <value>400</value>
- <value>500</value>
- </list>
- </property>
- <property name="testlist">
- <list>
- <value>1.1111</value>
- <value>true</value>
- <value>string</value>
- <value>100</value>
- <ref bean="testok" />
- </list>
- </property>
- <property name="testmap">
- <map>
- <entry key="1">
- <value>1.1111</value>
- </entry>
- <entry key="2">
- <value>true</value>
- </entry>
- <entry key="3">
- <value>string</value>
- </entry>
- <entry key="4">
- <value>100</value>
- </entry>
- <entry key="5">
- <ref bean="testok" />
- </entry>
- </map>
- </property>
- <property name="testset">
- <set>
- <value>1.1111</value>
- <value>true</value>
- <value>string</value>
- <value>100</value>
- <ref bean="testok" />
- </set>
- </property>
- <property name="prop">
- <props>
- <prop key="1">1.1111</prop>
- <prop key="2">true</prop>
- <prop key="3">string</prop>
- <prop key="4">100</prop>
- <prop key="5">testok</prop>
- </props>
- </property>
- </bean>
- </beans>
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集合、类,甚至可以是应用程序中使用到的资源文件。