Spring Recipes 2

配置集合属性

     1.List 实例代码(SequenceGenerator.java + beans.xml + Main.java):

        SequenceGenerator.java

package com.apress.springrecipes.sequence;

import java.util.List;


public class SequenceGenerator {

    private List<Object> suffixes;
    
    private int initial;
    
    private int counter;
    
    public SequenceGenerator() {}

    public void setSuffixes(List<Object> suffixes) {
        this.suffixes = suffixes;
    }

    public void setInitial(int initial) {
        this.initial = initial;
    }

    public synchronized String getSequence() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(initial + counter++);
        for (Object suffix : suffixes) 
             buffer.append("-").append(suffix);
        return buffer.toString();
    }
}

        beans.xml

<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-3.0.xsd">

    <bean id="sequenceGenerator" class="com.apress.springrecipes.sequence.SequenceGenerator">
        <property name="initial" value="100000"/>
        <property name="suffixes">
            <list>
                <value>A</value>
                <bean class="java.net.URL">
                    <constructor-arg value="http"/>
                    <constructor-arg value="www.apress.com"/>
                    <constructor-arg value="/"/>
                </bean>
                <null />
            </list>
        </property>
    </bean>
</beans>

       Main.java

package com.apress.springrecipes.sequence;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Main {
 
    public static void main(String[] args) {
        ApplicationContext context = 
            new ClassPathXmlApplicationContext("beans.xml");
 
        SequenceGenerator generator =
            (SequenceGenerator) context.getBean("sequenceGenerator");
 
        System.out.println(generator.getSequence());
        System.out.println(generator.getSequence());
    }
}

        控制台输出结果

100000-A-http://www.apress.com/-null
100001-A-http://www.apress.com/-null


    2.Array 实例代码(SequenceGenerator.java + beans.xml + Main.java):

只需要修改SequenceGenerator.java,其它代码同List

        SequenceGenerator.java

package com.apress.springrecipes.sequence;



public class SequenceGenerator {

    private Object[] suffixes;
    
    private int initial;
    
    private int counter;
    
    public SequenceGenerator() {}
    
    public void setSuffixes( Object[] suffixes) {
        this.suffixes = suffixes;
    }
    public void setInitial(int initial) {
        this.initial = initial;
    }

    public synchronized String getSequence() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(initial + counter++);
        for (Object suffix : suffixes) 
             buffer.append("-").append(suffix);
        return buffer.toString();
    }
}

        beans.xml(同上,1. List)

        Main.java(同上,1. List)

        控制台输出结果(同上,1. List)

    3.Set 实例代码(SequenceGenerator.java + beans.xml + Main.java):

        SequenceGenerator.java

package com.apress.springrecipes.sequence;

import java.util.Set;



public class SequenceGenerator {

    private Set<Object> suffixes;
    
    private int initial;
    
    private int counter;
    
    public SequenceGenerator() {}
    
    public void setSuffixes( Set<Object> suffixes) {
        this.suffixes = suffixes;
    }
    public void setInitial(int initial) {
        this.initial = initial;
    }

    public synchronized String getSequence() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(initial + counter++);
        for (Object suffix : suffixes) 
             buffer.append("-").append(suffix);
        return buffer.toString();
    }
}

         beans.xml,注:Spring默认使用LinkedHashSet所以保证了顺序

<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-3.0.xsd">

    <bean id="sequenceGenerator" class="com.apress.springrecipes.sequence.SequenceGenerator">
        <property name="initial" value="100000"/>
        <property name="suffixes">
            <set>
                <value>A</value>
                <bean class="java.net.URL">
                    <constructor-arg value="http"/>
                    <constructor-arg value="www.apress.com"/>
                    <constructor-arg value="/"/>
                </bean>
                <null />
            </set>
        </property>
    </bean>
</beans>

        Main.java(同上,1. List)

        控制台输出结果(同上,1. List)

    4.Map 实例代码(SequenceGenerator.java + beans.xml + Main.java):

        SequenceGenerator.java

package com.apress.springrecipes.sequence;

import java.util.Map;



public class SequenceGenerator {

    private Map<Object, Object> suffixes;
    
    private int initial;
    
    private int counter;
    
    public SequenceGenerator() {}
    
    public void setSuffixes( Map<Object, Object> suffixes) {
        this.suffixes = suffixes;
    }
    public void setInitial(int initial) {
        this.initial = initial;
    }

    public synchronized String getSequence() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(initial + counter++);
        for (Map.Entry entry : suffixes.entrySet()) 
             buffer.append("-")
                 .append(entry.getKey())
                 .append("@")
                 .append(entry.getValue());
        return buffer.toString();
    }
}

         beans.xml

<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-3.0.xsd">

    <bean id="sequenceGenerator" class="com.apress.springrecipes.sequence.SequenceGenerator">
        <property name="initial" value="100000"/>
        <property name="suffixes">
            <map>
                <entry>
                    <key>
                        <value>type</value>
                    </key>
                    <value>A</value>
                </entry>
                <entry>
                    <key>
                        <value>url</value>
                    </key>
                    <bean class="java.net.URL">
                        <constructor-arg value="http"/>
                        <constructor-arg value="www.apress.com"/>
                        <constructor-arg value="/"/>
                    </bean>
                </entry>
            </map>
        </property>
    </bean>
</beans>

        beans.xml,简写

<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-3.0.xsd">

    <bean id="sequenceGenerator" class="com.apress.springrecipes.sequence.SequenceGenerator">
        <property name="initial" value="100000"/>
        <property name="suffixes">
            <map>
                <entry key="type" value="A"/>
                <entry key="url">
                    <bean class="java.net.URL">
                        <constructor-arg value="http"/>
                        <constructor-arg value="www.apress.com"/>
                        <constructor-arg value="/"/>
                    </bean>
                </entry>
            </map>
        </property>
    </bean>
</beans>

        Main.java(同上,1. List)

        控制台输出结果

100000-type@A-url@http://www.apress.com/
100001-type@A-url@http://www.apress.com/

        注:配置空值Map

<property name="nulledMapValue">
    <map>
        <entry>
            <key><value>null</value></key>
        </entry>
    </map>
</property>

    5.Properties 实例代码(SequenceGenerator.java + beans.xml + Main.java):

        SequenceGenerator.java

package com.apress.springrecipes.sequence;

import java.util.Map;
import java.util.Properties;



public class SequenceGenerator {

    private Properties suffixes;
    
    private int initial;
    
    private int counter;
    
    public SequenceGenerator() {}
    
    public void setSuffixes(Properties suffixes) {
        this.suffixes = suffixes;
    }
    public void setInitial(int initial) {
        this.initial = initial;
    }

    public synchronized String getSequence() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(initial + counter++);
        for (Map.Entry entry : suffixes.entrySet()) 
             buffer.append("-")
                 .append(entry.getKey())
                 .append("@")
                 .append(entry.getValue());
        return buffer.toString();
    }
}

        beans.xml

<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-3.0.xsd">

    <bean id="sequenceGenerator" class="com.apress.springrecipes.sequence.SequenceGenerator">
        <property name="initial" value="100000"/>
        <property name="suffixes">
            <props>
                <prop key="type">A</prop>
                <prop key="url">http://www.apress.com/</prop>
                <prop key="null">null</prop>
            </props>
        </property>
    </bean>
</beans>

        Main.java(同上,1. List)

        控制台输出结果

100000-null@null-url@http://www.apress.com/-type@A
100001-null@null-url@http://www.apress.com/-type@A

合并父Bean集合

    1.List 实例代码(SequenceGenerator.java + beans.xml + Main.java):

        SequenceGenerator.java(同上,1. List)

        beans.xml

<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-3.0.xsd">

    <bean id="baseSequenceGenerator" class="com.apress.springrecipes.sequence.SequenceGenerator">
        <property name="initial" value="100000"/>
        <property name="suffixes">
            <list>
                <value>A</value>
                <value>B</value>
            </list>
        </property>
    </bean>
    <bean id="sequenceGenerator" parent="baseSequenceGenerator">
        <property name="suffixes">
            <list merge="true">
                <value>A</value>
                <value>C</value>
            </list>
        </property>
    </bean>
</beans>

        Main.java(同上,1. List)

        控制台输出结果

100000-A-B-A-C
100001-A-B-A-C

    2.Set,Map 实例代码(SequenceGenerator.java + beans.xml + Main.java):

        注:Set,Map如果值相同,子元素将覆盖父元素。

        SequenceGenerator.java(同上,3. Set)

        beans.xml

<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-3.0.xsd">

    <bean id="baseSequenceGenerator" class="com.apress.springrecipes.sequence.SequenceGenerator">
        <property name="initial" value="100000"/>
        <property name="suffixes">
            <set>
                <value>A</value>
                <value>B</value>
            </set>
        </property>
    </bean>
    <bean id="sequenceGenerator" parent="baseSequenceGenerator">
        <property name="suffixes">
            <set merge="true">
                <value>A</value>
                <value>C</value>
            </set>
        </property>
    </bean>
</beans>

        Main.java(同上,1. List)

        控制台输出结果

100000-A-B-C
100001-A-B-C

        SequenceGenerator.java(同上,4. Map)

        beans.xml

<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-3.0.xsd">

    <bean id="baseSequenceGenerator" class="com.apress.springrecipes.sequence.SequenceGenerator">
        <property name="initial" value="100000"/>
        <property name="suffixes">
            <map>
                <entry key="A" value="A"/>
                <entry key="B" value="B"/>
            </map>
        </property>
    </bean>
    <bean id="sequenceGenerator" parent="baseSequenceGenerator">
        <property name="suffixes">
            <map>
                <entry key="A" value="a"/>
                <entry key="C" value="C"/>
            </map>
        </property>
    </bean>
</beans>

        Main.java(同上,1. List)

        控制台输出结果

100000-A@a-C@C
100001-A@a-C@C


你可能感兴趣的:(Spring Recipes 2)