在spring当中如何对不同的类进行注入呢:
在MyBean当中定义了不同类型;如何对他们注入呢;
对于boolean/float/String中注入方法为<property name="str" value="hello"></property>
引用类型的注入方法为: <property name="picDao" ref="picDao"></property>
list类型的注入方式为;
<property name="list"><list><value>1</value><ref bean="picDao" /></list></property>
set集合的配置:
<property name="set">
<set>
<value>abc</value>
<ref bean="picDao" />
</set>
</property> map集合的配置:
<property name="map">
<map>
<entry key="1" value="abc"></entry>
<entry key-ref="picDao" value-ref="picDao"></entry>
</map>
</property>
数组的配置:
<property name="arr">
<list>
<value>1</value>
<value>2</value>
</list>
</property>
properties的配置:
<!-- 必须是字符串 -->
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">rot</prop>
</props>
</property> 如何对自定义的类型进行注入:利用对自定义的Date类型;
1、首先添加一个<property name="d" value="2008/08/08"></property> 标签,value写自己的时期时间;
2、创建一个DateEditor类继承PropertyEditorSupport 类,重写setAsText 方法;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateEditor extends PropertyEditorSupport{
@Override
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
try {
Date d = sdf.parse(text);
this.setValue(text);
} catch (ParseException e) {
e.printStackTrace();
}
}
} <bean id="dateEditor" class="com.pk.convert.DateEditor"></bean>
4、配置一个解析自定义类的一个标签
<bean id="CustomEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date" value-ref="dateEditor"></entry>
</map>
</property>
</bean> package com.pk.dao;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class MyBean {
private boolean b;
private float f;
private PicDao picDao;
private String str;
private List list;
private Set set;
private Map map;
private int[] arr;
private Properties properties;
private Date d;
public boolean isB() {
return b;
}
public void setB(boolean b) {
this.b = b;
}
public float getF() {
return f;
}
public void setF(float f) {
this.f = f;
}
public PicDao getPicDao() {
return picDao;
}
public void setPicDao(PicDao picDao) {
this.picDao = picDao;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public int[] getArr() {
return arr;
}
public void setArr(int[] arr) {
this.arr = arr;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Date getD() {
return d;
}
public void setD(Date d) {
this.d = d;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="picDao" class="com.pk.dao.PicDao"></bean>
<bean id="MyBean" class="com.pk.dao.MyBean">
<!-- 基本类型的配置 -->
<property name="b" value="true"></property>
<property name="f" value="3.14"></property>
<property name="picDao" ref="picDao"></property>
<property name="str" value="hello"></property>
<!-- list类型配置 -->
<property name="list">
<list>
<value>1</value>
<ref bean="picDao" />
</list>
</property>
<!-- 配置set集合 -->
<property name="set">
<set>
<value>abc</value>
<ref bean="picDao" />
</set>
</property>
<property name="map">
<map>
<entry key="1" value="abc"></entry>
<entry key-ref="picDao" value-ref="picDao"></entry>
</map>
</property>
<property name="arr">
<list>
<value>1</value>
<value>2</value>
</list>
</property>
<!-- 必须是字符串 -->
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">rot</prop>
</props>
</property>
<property name="d" value="2008/08/08"></property>
</bean>
<bean id="dateEditor" class="com.pk.convert.DateEditor"></bean>
<bean id="CustomEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date" value-ref="dateEditor"></entry>
</map>
</property>
</bean>
</beans>