您的位置:首页 > 编程语言 > Java开发

Spring自动类型转换/集合属性注入

2015-06-24 11:26 501 查看
自动类型转换,如自动将”1991-01-01”字符串转换成Date格式

集合属性注入:给集合注入值

FlashDisk

package net.wchdai.spring.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import net.wchdai.spring.dao.Disk;

public class FlashDisk implements Disk {

private String mDiskName;
private int mDiskCapacity;
private Date mMadeDate;

private Map<String, String> mDataMap = new HashMap<String, String>();
private List<Integer> mDataList = new ArrayList<Integer>();
private Properties mProperty = new Properties();
private Set<String> mDataSet = new HashSet<String>();

public FlashDisk(){
System.out.println("FlashDisk constructor.");
}

public Date getmMadeDate() {
return mMadeDate;
}

public void setmMadeDate(Date mMadeDate) {
System.out.println("setMadeDate");
this.mMadeDate = mMadeDate;
}
public Map<String, String> getmDataMap() {
return mDataMap;
}

public void setmDataMap(Map<String, String> mDataMap) {
this.mDataMap = mDataMap;
}

public List<Integer> getmDataList() {
return mDataList;
}

public void setmDataList(List<Integer> mDataList) {
this.mDataList = mDataList;
}

public Properties getmProperty() {
return mProperty;
}

public void setmProperty(Properties mProperty) {
this.mProperty = mProperty;
}

public Set<String> getmDataSet() {
return mDataSet;
}

public void setmDataSet(Set<String> mDataSet) {
this.mDataSet = mDataSet;
}

public String getmDiskName() {
return mDiskName;
}

public void setmDiskName(String mDiskName) {
this.mDiskName = mDiskName;
}

public int getmDiskCapacity() {
return mDiskCapacity;

}

public void setmDiskCapacity(int mDiskCapacity) {
this.mDiskCapacity = mDiskCapacity;
}

@Override
public void read() {
System.out.println("flash disk read...");
}

@Override
public void write(String data) {
System.out.println("flash disk write:" + data);
}

@Override
public String printInfo() {
return "FlashDisk " + getmDiskName() + ", capacity:"
+ getmDiskCapacity();
}

public void saveUser() {
System.out.println("Set集合注入");
for (String str : mDataSet) {
System.out.println(str);
}

System.out.println("------------------------------");
System.out.println("Map集合注入");
for (String str : mDataMap.values()) {
System.out.println(str);
}

System.out.println("------------------------------");
System.out.println("Properties属性集合注入");
for (Object str : mProperty.values()) {
System.out.println(str);
}

System.out.println("------------------------------");
System.out.println("List集合注入");
for (Integer i : mDataList) {
System.out.println(i);
}
}
}


beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

<bean name="flashdisk" class="net.wchdai.spring.dao.impl.FlashDisk">
<property name="mDiskName" value="wchdai'sFlashDisk" />
<property name="mDiskCapacity" value="17" />
<property name="mMadeDate" value="2007-10-01"/>

<property name="mDataSet">
<set>
<value>"HashSetValue1"</value>
<value>"HashSetValue2"</value>
<value>"HashSetValue3"</value>
</set>
</property>
<property name="mDataMap">
<map>
<entry key="mDataMapKey1" value="mDataMapValue1"></entry>
<entry key="mDataMapKey2" value="mDataMapValue2"></entry>
<entry key="mDataMapKey3" value="mDataMapValue3"></entry>
</map>
</property>
<property name="mDataList">
<list>
<value>12</value>
<value>13</value>
<value>14</value>
</list>
</property>
<property name="mProperty">
<props>
<prop key="mProperty1">1</prop>
<prop key="mProperty2">2</prop>
<prop key="mProperty3">3</prop>
</props>
</property>
</bean>

<bean name="customEditor"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="net.wchdai.spring.util.DateConvert"></bean>
</entry>
</map>
</property>
</bean>
</beans>


TestDisk.java

package net.wchdai.spring;

import net.wchdai.spring.dao.impl.FlashDisk;

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

import junit.framework.TestCase;

public class TestDisk extends TestCase{
public void testDisk(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
FlashDisk disk = (FlashDisk)ctx.getBean("flashdisk");

}
}


运行结果:

六月 24, 2015 11:25:01 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@61201a5c: startup date [Wed Jun 24 11:25:01 CST 2015]; root of context hierarchy
六月 24, 2015 11:25:01 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
六月 24, 2015 11:25:01 上午 org.springframework.beans.factory.config.CustomEditorConfigurer postProcessBeanFactory
警告: Passing PropertyEditor instances into CustomEditorConfigurer is deprecated: use PropertyEditorRegistrars or PropertyEditor class names instead. Offending key [java.util.Date; offending editor instance: net.wchdai.spring.util.DateConvert@10e73e0
六月 24, 2015 11:25:01 上午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@25286ff8: defining beans [flashdisk,customEditor]; root of factory hierarchy
FlashDisk constructor.
convert 2007-10-01 to Data format.
setMadeDate
Set集合注入
"HashSetValue1"
"HashSetValue2"
"HashSetValue3"
------------------------------
Map集合注入
mDataMapValue1
mDataMapValue2
mDataMapValue3
------------------------------
Properties属性集合注入
1
3
2
------------------------------
List集合注入
12
13
14
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: