通过Setters方式进行普通属性的IOC注入

IOC的依赖注入方式有: 

³构造器注入:通过类的构造方法注入依赖关系 
设值方法注入:通过类的setters方法注入依赖关系 
接口注入(不常用):定义一个注入接口,在需要注入的类中实现此接口,由于这种方法具有侵入性,所以不常用
 
1、使用到的类: CommonPropertyInjection.java

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;

    }

   

}

 

 
2、 applicationContext.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.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>

 
3、单元测试类 InjectionTest.java

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}

 

 

                                                       --------------此实例设计思想以及代码大部分来自北京尚学堂

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值