Spring Recipes 2

本文详细介绍了Spring框架中如何进行集合类型的注入,包括List、Array、Set、Map和Properties等不同类型的配置方式,并演示了如何通过合并父Bean实现集合的扩展。

配置集合属性

     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


转载于:https://my.oschina.net/wtm/blog/306976

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值