spring 讲对象的创建和依赖关系交给spring(IOC容器)来管理
srping实现的IOC 由DI实现(注入管理)
普通属性.日期的注入
外界想引用该类,都需要引用id,由id对应该引用类
如果不是类.不用ref 用value直接赋值
下面这个例子讲的是spring中对普通属性如何注入,对日期类型如何注入.
package zhc.love.dj.POJO;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Bean {
private String strvalue ;
private int intvalue ;
private List listvalue ;
private Set setvalue ;
private String[] arrayvalue ;
private Map mapvalue ;
//不支持日期类型
//得增加转换代码.用属性编辑器
//使用java.beans.PropertyEditorSupport类 SetAsText()方法
//
private Date datavalue ;
public String[] getArrayvalue() {
return arrayvalue;
}
public void setArrayvalue(String[] arrayvalue) {
this.arrayvalue = arrayvalue;
}
public int getIntvalue() {
return intvalue;
}
public void setIntvalue(int intvalue) {
this.intvalue = intvalue;
}
public List getListvalue() {
return listvalue;
}
public void setListvalue(List listvalue) {
this.listvalue = listvalue;
}
public Map getMapvalue() {
return mapvalue;
}
public void setMapvalue(Map mapvalue) {
this.mapvalue = mapvalue;
}
public Set getSetvalue() {
return setvalue;
}
public void setSetvalue(Set setvalue) {
this.setvalue = setvalue;
}
public String getStrvalue() {
return strvalue;
}
public void setStrvalue(String strvalue) {
this.strvalue = strvalue;
}
public Date getDatavalue() {
return datavalue;
}
public void setDatavalue(Date datavalue) {
this.datavalue = datavalue;
}
}
Converter类: 将String类型转换为对象
package zhc.love.dj.POJO;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Converter extends PropertyEditorSupport {
private String format = "yyyy-MM-dd";
//按照格式将string类型转换为指定的日期类型
/*
什么是属性编辑器,作用?
自定义属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入
spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器
如何定义属性编辑器?
* 继承PropertyEditorSupport类,覆写setAsText()方法,
* 将属性编辑器注册到spring中,参见:applicationContextconverter.xml
*/
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date date = sdf.parse(text);
this.setValue(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
TestCase 类 : Junit测试单元
package zhc.love.dj.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zhc.love.dj.POJO.Bean;
public class TestCase extends junit.framework.TestCase {
public void testSpringValue(){
// 用spring的类来加载配置文件
//xml名字要与配置的名字相同,可以改变.
// 读取配置文件的时候支持通配符,可以有多个配置文件,但是配置文件中的id不能相同
// 读取的配置文件支持数组读取
// String[] config = new String[]{"a.xml",b.xml}
//BeanFactory factory = new ClassPathXmlApplicationContext(config);
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext*.xml");
Bean bean = (Bean)factory.getBean("bean");
System.out.println("string value : "+bean.getStrvalue());
System.out.println("int value :" + bean.getIntvalue());
System.out.println("list value: "+ bean.getListvalue());
System.out.println("set value:" + bean.getSetvalue()) ;
System.out.println("array value :" + bean.getArrayvalue());
System.out.println("map value :" +bean.getMapvalue());
System.out.println("date value :" + bean.getDatavalue());
}
}
配置转换类的xml文件
<?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-2.0.xsd">
<!--讲自己写的类注入到spring中,将自己的字符串转换为日期类型 -->
<bean id="CustomEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="zhc.love.dj.POJO.Converter"></bean>
</entry>
</map>
</property>
</bean>
</beans>
配置spring的文件
<?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-2.0.xsd">
<bean id="bean" class="zhc.love.dj.POJO.Bean">
<!-- set方法所设置的属性 ,id不能重复-->
<!-- 把实例化的事交给bean工厂来做 -->
<property name="strvalue" value="I love you"></property>
<!-- 两种形式给属性赋值 -->
<property name="intvalue">
<value>1314</value>
</property>
<property name="listvalue">
<list>
<value>list1</value>
<value>list2</value>
</list>
</property>
<property name="setvalue">
<set>
<value>set1</value>
<value>set2</value>
</set>
</property>
<property name="arrayvalue">
<list>
<value>arraylist1</value>
<value>arraylist2</value>
</list>
</property>
<property name="mapvalue">
<map>
<entry key="key1" value="mapvalue1"></entry>
<entry key="key2" value="mapvalue2"></entry>
</map>
</property>
<property name="datavalue">
<value>2008-08-08</value>
</property>
</bean>
</beans>