关闭

集合对象注入

514人阅读 评论(0) 收藏 举报

在项目开发中,还是经常会用到集合对象注入的,不过感觉没有多少需要介绍的,这次就只给出实例了。
 
· 先建立一个包:javamxj.spring.basic.collections ,然后把以下4个文件放在这个包下。

==============================================================================
Hello.java

package javamxj.spring.basic.collections;

public class Hello {
    public String toString() {
        return "这是一个bean";
    }
}
==============================================================================
CollectionInjection.java

package javamxj.spring.basic.collections;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class CollectionInjection {
    private Map someMap;

    private Properties someProps;

    private Set someSet;

    private List someList;

    public void setSomeProps(Properties someProps) {
        this.someProps = someProps;
    }

    public void setSomeList(List someList) {
        this.someList = someList;
    }

    public void setSomeMap(Map someMap) {
        this.someMap = someMap;
    }

    public void setSomeSet(Set someSet) {
        this.someSet = someSet;
    }

    public void displayInfo() {

        System.out.println("Map contents:");
        for (Object key : someMap.keySet()) {
            System.out.println("Key: " + key + " - Value: " + someMap.get(key));
        }

        System.out.println("/nProperties contents:");
        for (Object key : someProps.keySet()) {
            System.out.println("Key: " + key + " - Value: "
                    + someProps.get(key));
        }

        System.out.println("/nSet contents:");
        for (Object i : someSet) {
            System.out.println("Value: " + i);
        }

        System.out.println("/nList contents:");
        for (Object i : someList) {
            System.out.println("Value: " + i);
        }

    }

==============================================================================
beans.xml

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   
    <bean id="hello" class="javamxj.spring.basic.collections.Hello"/>   

    <bean id="injectCollection"
        class="javamxj.spring.basic.collections.CollectionInjection">
        <property name="someMap">
            <map>
                <entry key="someValue" value="Hello javamxj!"/>
                <entry key="someBean">
                    <ref local="hello"/>
                </entry>
            </map>
        </property>
       
        <property name="someProps">
            <props>
                <prop key="姓">张</prop>
                <prop key="名">小小</prop>
            </props>
        </property>
       
        <property name="someSet">
            <set>
                <value>Hello World!</value>
                <ref local="hello"/>
            </set>
        </property>
       
        <property name="someList">
            <list>
                <value>Hello World!</value>
                <ref local="hello"/>
            </list>
        </property>
       
    </bean>
</beans> 

==============================================================================
Main.java
package javamxj.spring.basic.collections;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Main {
    public static void main(String[] args) {
        BeanFactory bf= new XmlBeanFactory(new ClassPathResource(
        "javamxj/spring/basic/collections/beans.xml"));

        CollectionInjection c = (CollectionInjection)bf.getBean("injectCollection");
        c.displayInfo();
    }
}

==============================================================================

·运行Main.java,输出结果如下:
 
Map contents:
Key: someValue - Value: Hello javamxj!
Key: someBean - Value: 这是一个bean
 
Properties contents:
Key: 姓 - Value: 张
Key: 名 - Value: 小小
 
Set contents:
Value: Hello World!
Value: 这是一个bean
 
List contents:
Value: Hello World!
Value: 这是一个bean
 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:173833次
    • 积分:3277
    • 等级:
    • 排名:第10749名
    • 原创:153篇
    • 转载:10篇
    • 译文:1篇
    • 评论:23条
    最新评论