Spring基础学习笔记 - - 集合类型注入方式

Java集合的几种类型的注入:

 List,
 Map,
 Set,
 Properties

一起看下Case,先定义一个JavaBean:

import java.util.*;
import com.zhongrun.spring.innerbean.Person;
public class Customer {

        private List<Object> lists ;
        private Set<Object> sets ;
        private Map<Object, Object> maps ;
        private Properties pros;
        public List<Object> getLists() {
            return lists;
        }
        public void setLists(List<Object> lists) {
            this.lists = lists;
        }
        public Set<Object> getSets() {
            return sets;
        }
        public void setSets(Set<Object> sets) {
            this.sets = sets;
        }
        public Map<Object, Object> getMaps() {
            return maps;
        }
        public void setMaps(Map<Object, Object> maps) {
            this.maps = maps;
        }
        public Properties getPros() {
            return pros;
        }
        public void setPros(Properties pros) {
            this.pros = pros;
        }
		//不要忘记写内部要引用的 Bean 
        private Person person;

        public Customer(Person person) {
            this.person = person;
        }

        public Customer(){}

        public void setPerson(Person person) {
            this.person = person;
        }

        @Override
        public String toString() {
            return "Customer information is [person=" + person + "]";
        }
}

需要进行注意的地方是:
JavaBean 关于属性命名的特殊规范,spring 配置文件中 元素所指定的属性名和 Bean 实现类的 Setter 方法满足 Sun JavaBean 的属性命名规范: xxx 的属性对应 setXxx() 方法。一般情况下 Java 的属性变量名都以小写字母起头,如: maxSpeed 。但也存在特殊的情况,考虑到一些特定意义的大写英文缩略词(如: USA 、 XML ),JavaBean 也允许大写字母起头的属性变量名,不过必须满足: 变量的前两个字母要么全部大写,要么全部小写.如: iC 、 iCcard 、 iDcode 这些都不合法的。

有关SpringBeans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--bean的 id 最好首字母小写 -->
    <bean id="customerBean"   class="com.<!--Set 与 List 类似-->.spring.collections.Customer">
        <!-- java.util.List -->
        <property name="lists">
            <list>
                <value>1</value><!-- List 属性既可以通过 <value> 注入字符串,也可以通过 <ref> 注入容器中其他的 Bean-->
                 <ref bean="personBean" />
                 <value>2</value>
                <bean class="com.shiyanlou.spring.collections.Person">
                    <property name="name" value="shiyanlouList" />
                    <property name="address" value="chengdu" />
                    <property name="age" value="25" />
                </bean>
            </list>
        </property>

        <!-- java.util.Set -->
        <property name="sets">
            <set>
            	<!--Set 与 List 类似-->
                <value>1</value>
                <ref bean="personBean" />
                <bean class="com.zhongrun.spring.collections.Person">
                    <property name="name" value="shiyanlouSet" />
                    <property name="address" value="chengdu" />
                    <property name="age" value="25" />
                </bean>
            </set>
        </property>

        <!-- java.util.Map -->
        <property name="maps">
            <map>
                <entry key="Key 1" value="1" /><!--一个 entry 就是一个 Map 元素-->
                <entry key="Key 2" value-ref="personBean" />
                <entry key="Key 3">
                    <bean class="com.zhongrun.spring.collections.Person">
                        <property name="name" value="shiyanlouMap" />
                           <property name="address" value="chengdu" />
                        <property name="age" value="25" />
                    </bean>
                </entry>
            </map>
        </property>    

        <!-- java.util.Properties -->
        <property name="pros"><!-- Properties 类型类似于Map 类型的特例,Map 元素的键值可以对应任何类型的对象,但是Properties只能是字符串-->
            <props>
                <prop key="admin">admin@nospam.com</prop>
                <prop key="support">support@nospam.com</prop>
            </props>
        </property>
    </bean>

    <bean id="personBean" class="com.zhongrun.spring.collections.Person">
        <property name="name" value="shiyanlouPersonBean" />
        <property name="address" value="chengdu" />
        <property name="age" value="25" />
    </bean>
</beans>

最后进行编写测试的APP:

package com.shiyanlou.spring.collections;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
     private static ApplicationContext context;

        public static void main( String[] args )
        {
            context = new ClassPathXmlApplicationContext("SpringBeans.xml");

            /*
             * case 1 : List 第1种情况
             */
            Customer customer = (Customer) context.getBean("customerBean");
            System.out.println(customer.getLists().toString());

            /*
             * case 2 : Set 第2种情况
             */
            System.out.println(customer.getSets().toString());

            /*
             * case 3 : Map 第3种情况
             */
            System.out.println(customer.getMaps().toString());

            /*
             * case 4 : 第4种情况
             */
            System.out.println(customer.getPros().toString());


        }
}

效果图:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值