`

Spring 属性注入(int ,String, List, Map, Set, String[])

    博客分类:
  • JAVA
阅读更多
1.JavaBean




import java.util.List;

import java.util.Map;

import java.util.Set;



public class Test {

    private int intValue;

    private String strValue;

    private List listValue;

    private Map mapValue;

    private Set<String> setValue;

    private String[] arrayValue;



    public int getIntValue() {

       return intValue;

    }



    public void setIntValue(int intValue) {

       this.intValue = intValue;

    }



    public String getStrValue() {

       return strValue;

    }



    public void setStrValue(String strValue) {

       this.strValue = strValue;

    }



    public List getListValue() {

       return listValue;

    }



    public void setListValue(List listValue) {

       this.listValue = listValue;

    }



    public Map getMapValue() {

       return mapValue;

    }



    public void setMapValue(Map mapValue) {

       this.mapValue = mapValue;

    }



    public Set getSetValue() {

       return setValue;

    }



    public void setSetValue(Set setValue) {

       this.setValue = setValue;

    }



    public String[] getArrayValue() {

       return arrayValue;

    }



    public void setArrayValue(String[] arrayValue) {

       this.arrayValue = arrayValue;

    }

}

2.Spring.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">



<beans>



    <bean id="TestBean" class="Test">

       <!-- 注入int数据 -->

       <property name="intValue" value="11" />



       <!-- 注入String数据 -->

       <property name="strValue">

           <value>Tom</value>

       </property>



       <!-- 注入List数据 -->

       <property name="listValue">

           <list>

              <value>list1</value>

              <value>list2</value>

              <value>list3</value>

           </list>

       </property>



       <!-- 注入Set数据 -->

       <property name="setValue">

           <set>

              <value>set1</value>

              <value>set2</value>

              <value>set3</value>

              <value>set4</value>

           </set>

       </property>



       <!-- 注入数组数据 -->

       <property name="arrayValue">

           <list>

              <value>array1</value>

              <value>array2</value>

              <value>array3</value>

              <value>array4</value>

           </list>

       </property>





       <!-- 注入map数据 -->

       <property name="mapValue">

           <map>

              <entry key="k1" value="map1" />

              <entry key="k2" value="map2" />

              <entry key="k3" value="map3" />

              <entry key="k4" value="map4" />

           </map>

       </property>

    </bean>



</beans>


3.测试bean





import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Set;

import java.util.Map.Entry;



import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;

import org.springframework.beans.factory.BeanFactory;



public class TestSpringDI {



    private static BeanFactory factory;



    public static void main(String[] args) {

       factory = new ClassPathXmlApplicationContext(

              "conf/spring/applicationContext1Test.xml");

       testBeanDemo();



    }



    public static void testBeanDemo() {

       Test test = (Test) factory.getBean("TestBean");



       System.out.println("beanDemo.intValue=" + test.getIntValue());



       System.out.println("beanDemo.strValue=" + test.getStrValue());

       List listValue = test.getListValue();

       System.out.println("beanDemo.listValue=");

       if (listValue != null) {

           for (Iterator it = listValue.iterator(); it.hasNext();) {

              System.out.println((String) it.next());

           }

       }



       System.out.println("beanDemo.setValue=");

       Set setValue = test.getSetValue();

       if (setValue != null) {

           for (Iterator it = setValue.iterator(); it.hasNext();) {

              System.out.println(it.next());

           }

       }



       System.out.println("beanDemo.arrayValue=");

       String[] arrayValue = test.getArrayValue();

       if (arrayValue != null) {

           for (int i = 0; i < arrayValue.length; i++) {

              System.out.println(arrayValue[i]);

           }

       }



       System.out.println("beanDemo.mapValue=");

       Map mapValue = test.getMapValue();

       if (mapValue != null) {

           Set<Map.Entry> set = mapValue.entrySet();

           for (Iterator it = set.iterator(); it.hasNext();) {

              Map.Entry entry = (Entry) it.next();

              System.out.println(entry.getKey() + " = " + entry.getValue());

           }

       }

    }

}


4.输出结果


beanDemo.intValue=11
beanDemo.strValue=Tom
beanDemo.listValue=
list1
list2
list3
beanDemo.setValue=
set1
set2
set3
set4
beanDemo.arrayValue=
array1
array2
array3
array4
beanDemo.mapValue=
k1 = map1
k2 = map2
k3 = map3
k4 = map4
分享到:
评论

相关推荐

    java面试宝典

    65、Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用==还是equals()? 它们有何区别 17 66、HashMap和Hashtable的区别 17 67、说出ArrayList,Vector, LinkedList的存储性能和特性 17 68、java中有几...

    springmybatis

    public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserAge()...

    Spring原理模拟代码

    String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]); m....

    Java代码实现依赖注入

    模仿Spring实现一种基于xml配置文件的依赖注入机制。文件中将实现3中注入,一是单值注入,包括int,float,double,char等,也...二是Java容器注入,包括List,Set,Map三种容器的注入,最后一种是java bean对象注入。

    jdbcTemplate

    public List&lt;User&gt; getUserLists(Map&lt;String, Object&gt; map) { return null; } @Override public Integer getUserCount(Map&lt;String, Object&gt; map) { String sql = "select count(1) from User where id=? ...

    java面试题

    list、set、map问题? 答:set 不允许重复,无序 list 允许重复,有序 map 成对的数据结构,键值必须具有唯一性 Servlet和CGI的区别? 答:Servlet与CGI的区别在于Servlet处于服务器进程中,它通过多线程方式允许...

    Java常见面试题208道.docx

    20.List、Set、Map 之间的区别是什么? 21.HashMap 和 Hashtable 有什么区别? 22.如何决定使用 HashMap 还是 TreeMap? 23.说一下 HashMap 的实现原理? 24.说一下 HashSet 的实现原理? 25.ArrayList 和 ...

    DWR.xml配置文件说明书(含源码)

    6、以上所有类型的集合,List,Set,Map(有些局限性) However nothing is added to the list of classes that can be created (i.e. put up for remoting) without you declaring it. 2、 The Converters DWR已经默认...

    基于Java的ORM框架Mongodb-ORM.zip

    Map&lt;String, String&gt; action = new HashMap&lt;String, String&gt;(); action.put("name", "yuxiangping-update"); int update = mongoT emplet.update("updateModel", model, action);  更多的使用方法...

    千方百计笔试题大全

    65、Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用==还是equals()? 它们有何区别 17 66、HashMap和Hashtable的区别 17 67、说出ArrayList,Vector, LinkedList的存储性能和特性 17 68、java中有几...

    最新Java面试宝典pdf版

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...

    Java面试笔试资料大全

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...

    Java面试宝典2010版

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 64、说出ArrayList,Vector, LinkedList的存储性能和特性 65、去掉一个Vector集合中重复的元素 66、Collection 和 Collections的区别。 67、Set里的元素...

    Java面试宝典-经典

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...

    JAVA面试宝典2010

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...

    java面试题大全(2012版)

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...

    Java面试宝典2012版

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67...

    java面试宝典2012

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 49 64、说出ArrayList,Vector, LinkedList的存储性能和特性 50 65、去掉一个Vector集合中重复的元素 50 66、Collection 和 Collections的区别。 51 67、Set...

    Java面试宝典2012新版

    63、List、Map、Set三个接口,存取元素时,各有什么特点? 45 64、说出ArrayList,Vector, LinkedList的存储性能和特性 46 65、去掉一个Vector集合中重复的元素 46 66、Collection 和 Collections的区别。 47 67、Set...

Global site tag (gtag.js) - Google Analytics