Spring攻略学习笔记(6)为集合元素指定数据类型

默认情况下,Spring将集合中所有元素作为字符串对待如果你不打算将集合元素作为字符串使用,就必须为他们指定数据类型。可以使用<value>标记的type属性指定每个集合元素的数据类型,也可以用集合标记的value-type属性指定所有元素的数据类型。

代码示例如下:

(1)SequenceGenerator类

/*
 * Copyright 2013-2015
 */
package com.jackie.codeproject.springrecipesnote.springioc;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Title: SequenceGenerator.java 
 * 序列生成器
 * 
 * @author jackie
 * @since Apr 13, 2013 12:56:57 PM
 * @version V1.0
 */
public class SequenceGenerator {

    /**
     * @Fields prefixGenerator : 前缀生成器
     */
    private PrefixGenerator prefixGenerator;
   
    /**
     * @Fields suffix : 后缀
     */
    private List<Object> suffixes;

    /**
     * @Fields initial : 初始值
     */
    private int initial;

    /**
      * @Fields counter : 计数器
      */
    private int counter;

    /**
      * 获取序列号,声明为同步,使其成为线程安全的方法
      * @author jackie  
      * @date Apr 13, 2013
      * @return    
      * @return String   
      */
    public synchronized String getSquence() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(prefixGenerator.getPrefix());
        buffer.append(initial + counter++);
        DecimalFormat formatter = new DecimalFormat("0000");
        for (Object suffix : suffixes) {
            buffer.append("-");
            buffer.append(formatter.format((Integer)suffix));
        }
        return buffer.toString();
    }

    /**
     * @param suffix
     *            the suffix to set
     */
    public void setSuffixes(List<Object> suffixes) {
        this.suffixes = suffixes;
    }

    /**
     * @param initial
     *            the initial to set
     */
    public void setInitial(int initial) {
        this.initial = initial;
    }

    /**
     * @param prefixGenerator the prefixGenerator to set
     */
    public void setPrefixGenerator(PrefixGenerator prefixGenerator) {
        this.prefixGenerator = prefixGenerator;
    }
}

(2)PrefixGenerator接口

/*
 * Copyright 2013-2015
 */
package com.jackie.codeproject.springrecipesnote.springioc;

/**
 * Title: PrefixGenerator.java 
 * 生成前缀
 * 
 * @author jackie
 * @since Apr 18, 2013 9:54:43 PM
 * @version V1.0
 */
public interface PrefixGenerator {
    public String getPrefix();
}

(3)日期前缀生成器

/*  
  * Copyright 2013-2015  
  */
package com.jackie.codeproject.springrecipesnote.springioc;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Title: DatePrefixGenerator.java
 *  日期前缀
 * 
 * @author jackie
 * @since Apr 18, 2013 10:03:02 PM
 * @version V1.0
 */
public class DatePrefixGenerator implements PrefixGenerator {
     private DateFormat formatter;
     
     public void setPattern(String pattern) {
        this.formatter = new SimpleDateFormat(pattern);
    }

    @Override
    public String getPrefix() {
        return formatter.format(new Date());
    }
}

(4)Bean配置文件

<?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-3.2.xsd">
    <bean id="datePrefixGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.DatePrefixGenerator">
       <property name="pattern" value="yyyyMMdd" />
    </bean>
    
    <bean id="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator">
       <property name="prefixGenerator" ref="datePrefixGenerator" />
       <property name="initial" value="100000" />
       <property name="suffixes">
          <list>
             <value>5</value>
             <value>10</value>
             <value>20</value>
          </list>
       </property>
    </bean>
</beans>

当运行程序时会抛出ClassCastException异常,因为类型是String,无法转换成整数。可以设置<value>标记的type属性指定每个集合元素的数据类型。

<bean id="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator">
   <property name="prefixGenerator" ref="datePrefixGenerator" />
   <property name="initial" value="100000" />
   <property name="suffixes">
      <list>
         <value type="int">5</value>
         <value type="int">10</value>
         <value type="int">20</value>
      </list>
   </property>
</bean>

也可以设置集合标记的value-type属性指定集合元素的数据类型。

<bean id="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator">
   <property name="prefixGenerator" ref="datePrefixGenerator" />
   <property name="initial" value="100000" />
   <property name="suffixes">
      <list value-type="int">
         <value>5</value>
         <value>10</value>
         <value>20</value>
      </list>
   </property>
</bean>

Java1.5或更高版本,可以使用类型安全的集合定义,即在声明集合便指定元素类型。

/*
 * Copyright 2013-2015
 */
package com.jackie.codeproject.springrecipesnote.springioc;

...........
public class SequenceGenerator {
   
    /**
     * @Fields suffix : 后缀
     */
    private List<Integer> suffixes;

    ..............

    /**
      * 获取序列号,声明为同步,使其成为线程安全的方法
      * @author jackie  
      * @date Apr 13, 2013
      * @return    
      * @return String   
      */
    public synchronized String getSquence() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(prefixGenerator.getPrefix());
        buffer.append(initial + counter++);
        DecimalFormat formatter = new DecimalFormat("0000");
        for (Object suffix : suffixes) {
            buffer.append("-");
            buffer.append(formatter.format(suffix));
        }
        return buffer.toString();
    }

    /**
     * @param suffix
     *            the suffix to set
     */
    public void setSuffixes(List<Integer> suffixes) {
        this.suffixes = suffixes;
    }

    ...............
}

一旦以类型安全的方式定义了集合,Spring就能够通过反射读取集合的类型信息,这样就不需要指定<list>的value-type属性。

<bean id="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator">
   <property name="prefixGenerator" ref="datePrefixGenerator" />
   <property name="initial" value="100000" />
   <property name="suffixes">
      <list>
         <value>5</value>
         <value>10</value>
         <value>20</value>
      </list>
   </property>
</bean>


原文地址:https://www.cnblogs.com/xinyuyuanm/p/3031474.html