Spring 注入枚举类型和日期类型
Cup类:
package Test201402;
import java.util.Date;
public class Cup {
private Long num;
private Color color;
private Date generator_date;
private Class<?> prop;
public Cup() {}
public Cup(Long num, Color color, Date generator_date) {
super();
this.num = num;
this.color = color;
this.generator_date = generator_date;
}
public Long getNum() {
return num;
}
public void setNum(Long num) {
this.num = num;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Date getGenerator_date() {
return generator_date;
}
public void setGenerator_date(Date generator_date) {
this.generator_date = generator_date;
}
public Class<?> getProp() {
return prop;
}
public void setProp(Class<?> prop) {
this.prop = prop;
}
}
1、 注入枚举类型
只要把枚举类型当成静态常量即可。
// java 类
package Test201402;
public enum Color {
red,
blue,
green,
orange,
white,
black
}
// 配置文件
<bean id="cup" class="Test201402.Cup">
<property name="num" value="1"/>
<property name="color" value="red"/>
</property>
</bean>
2、 注入日期类型
由于日期类型不是常规类型,需要通过java.text.DateFormat.parse()解析,或者通过重写spring熟悉方法。
①、利用工厂注入方式,通过java.text.DateFormat.parse()解析日期字符串
// 配置文件
<!-- 方式一:利用工厂注入方式,通过java.text.DateFormat.parse()解析日期字符串, -->
<bean id="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg index="0" value="yyyy-MM-dd"/>
</bean>
<bean id="cup" class="Test201402.Cup">
<property name="num" value="1"/>
<property name="color" value="red"/>
<property name="generator_date" >
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="2014-02-10"/>
</bean>
</property>
</bean>
②、通过重写spring熟悉方法,首先需要继承PropertyEditorSupport类重写setAsText方法,然后配置xml如下
继承PropertyEditorSupport类:
package Test201402;
import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyCustomerEditor extends PropertyEditorSupport{
private String format;
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
DateFormat df = new SimpleDateFormat(format);
try {
Date date = df.parse(text);
this.setValue(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
测试类:
package Test201402;
import static org.junit.Assert.assertEquals;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.junit.Before;
import org.junit.Test;
import Test201309.BaseTest;
public class Test20140210 extends BaseTest{
@Test
public void testDate(){
Cup cup = bf.getBean("cup",Cup.class);
assertEquals(cup.getColor(),Color.red);
assertEquals(cup.getNum(),1L);
Calendar calendar = Calendar.getInstance();
calendar.set(2014, 1, 10, 0, 0, 0);
assertEquals(cup.getGenerator_date(),calendar.getTime());
}
@Before
public void init(){
this.setBf("conf/config_20140210.xml");
}
}
// 配置文件
<bean id="cup" class="Test201402.Cup">
<property name="num" value="1"/>
<property name="color" value="red"/>
<property name="generator_date" >
<value>2014-02-10</value>
</property>
</bean>
<!-- 方式二:通过重写spring熟悉方法,首先需要继承PropertyEditorSupport类重写setAsText方法,然后配置xml如下-->
<bean id="customDateEditor" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="Test201402.MyCustomerEditor">
<property name="format" value="yyyy-MM-dd"/>
</bean>
</entry>
</map>
</property>
</bean>