Kela's Blog

            前面的路很坎坷,但毕竟是条路.也许走过这一段就会发现,走过去就是梦想中的地方.因此坚持成为此刻唯一能做且必须去做的事情.
posts - 9, comments - 27, trackbacks - 0, articles - 15

kela的笔记 应用程序框架 ---- spring(4)

Posted on 2006-08-22 12:28 Kela 阅读(189) 评论(0)  编辑  收藏 所属分类: 我的笔记(Spring)

摘要:关于集合对象的注入(ArrayListMap

 

     SomeBean.java

 

import java.util.List;

import java.util.Map;

 

public class SomeBean {

    private String[] someStrArray;

    private Some[] someObjArray;

    private List someList;

    private Map someMap;

   

    public List getSomeList() {

        return someList;

    }

    public void setSomeList(List someList) {

        this.someList = someList;

    }

    public Map getSomeMap() {

        return someMap;

    }

    public void setSomeMap(Map someMap) {

        this.someMap = someMap;

    }

    public Some[] getSomeObjArray() {

        return someObjArray;

    }

    public void setSomeObjArray(Some[] someObjArray) {

        this.someObjArray = someObjArray;

    }

    public String[] getSomeStrArray() {

        return someStrArray;

    }

    public void setSomeStrArray(String[] someStrArray) {

        this.someStrArray = someStrArray;

    }

}

     Some.java

 

package com.kela.spring.ioc;

 

public class Some {

    private String name;

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

   

    public String toString() {

        return name;

    }

}

     Beans-config_3.xml

 

<?xml version= "1.0" encoding= "GB2312" ?>

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

 

<beans>

    <bean id= "some_1" class= "com.kela.spring.ioc.Some" >

       <property name= "name" >

           <value> KangFeng </value>

       </property>

    </bean>

   

    <bean id= "some_2" class= "com.kela.spring.ioc.Some" >

       <property name= "name" >

           <value> Kela </value>

       </property>

    </bean>

   

    <bean id= "someBean" class= "com.kela.spring.ioc.SomeBean" >

      

       <!-- 数组 -->

       <property name= "someStrArray" >

           <list>

              <value> Hello </value>

              <value> 你好 </value>

           </list>

       </property>

      

       <!-- 对象数组 -->

       <property name= "someObjArray" >

           <list>

              <ref bean= "some_1" ></ref>

              <ref bean= "some_2" ></ref>

           </list>

       </property>

      

       <!-- List -->

       <property name= "someList" >

           <list>

              <value> ListTest </value>

              <ref bean= "some_1" ></ref>

              <ref bean= "some_2" ></ref>

           </list>

       </property>

      

       <!-- Map -->

       <property name= "someMap" >

           <map>

              <entry key= "MapTest" >

                  <value> 你好, KangFeng </value>

              </entry>

              <entry key= "someKey_1" >

                  <ref bean= "some_1" ></ref>

              </entry>

           </map>

       </property>

    </bean>

</beans>

     TestClass.java

 

package com.kela.spring.ioc;

 

import java.util.List;

import java.util.Map;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

 

public class TestClass {

   

    public void method_3() {

        try {

            ApplicationContext context = new FileSystemXmlApplicationContext("bin\\com\\kela\\spring\\ioc\\beans-config_3.xml");

            SomeBean someBean = (SomeBean)context.getBean("someBean");

           

            // 取得StringArray注入的内容

            System.out.println(" 打印StringArray的内容~~~");

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

                System.out.println(someBean.getSomeStrArray()[i]);

            }

           

            // 取得SomeObject注入的内容

            System.out.println(" 打印SomeObject的内容~~~");

            Some[] some = (Some[])someBean.getSomeObjArray();

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

                System.out.println(some[i].getName());

            }

           

            // 取得List注入的内容

            System.out.println(" 打印List的内容~~~");

            List someList = (List)someBean.getSomeList();

            for(int i = 0; i < someList.size(); i++) {

                System.out.println(someList.get(i));

            }

           

            // 取得Map注入的内容

            System.out.println(" 打印Map的内容~~~");

            Map someMap = (Map)someBean.getSomeMap();

            System.out.println(someMap.get("MapTest"));

            System.out.println(someMap.get("someKey_1"));

        } catch (Exception e) {

            System.out.println("[ERROR]" + e.getMessage());

        }

    }

   

    public static void main(String[] args) {

        TestClass testClass = new TestClass();

        testClass.method_3();

    }

}


只有注册用户登录后才能发表评论。


网站导航:
 
分享到: