发布于 2016-01-01 10:57:19 | 155 次阅读 | 评论: 0 | 来源: PHPERZ
Spring Framework 开源j2ee框架
Spring是什么呢?首先它是一个开源的项目,而且目前非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层 中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transcation Managment,等等
对于对象的注入,我们使用ref方式,可以指定注入的对象,下面看下对于基本类型的注入,以及当spring无法转换基本类型进行注入时,如何编写一个类似转换器的东西来完成注入。
下面写一个简单类,来看下spring中对于基本类型的注入:
<bean id="bean1" class="com.shuitian.spring.Bean1">
<!-- <property name="strValue" value="hello_spring"/> --><!-- 也可以使用下面的方式 -->
<!-- private String strValue -->
<property name="strValue">
<value>hello_spring</value>
</property>
<!-- private int intValue; -->
<property name="intValue" value="123"/>
<!-- private List listValue; -->
<property name="listValue">
<list>
<value>list1</value>
<value>list2</value>
</list>
</property>
<!-- private Set setValue; -->
<property name="setValue">
<set>
<value>set1</value>
<value>set2</value>
</set>
</property>
<!-- private String[] arrayValues; -->
<property name="arrayValues">
<list>
<value>array1</value>
<value>array2</value>
</list>
</property>
<!-- private Map mapValue; -->
<property name="mapValue">
<map>
<entry key="k1" value="v1"/>
<entry key="k2" value="v2"/>
</map>
</property>
</bean>
在测试类中加入java.util.Date:
配置:
<property name="dateValue" value="2009-12-12"/>
如果像前面那样配置dataValue,为他注入值,会因为string在转换Date的时候spring无法识别util.Date而报错,所以,我们要自己定义一个类,来将如果转换的这一过程写下来。
/*
* java.util.date属性编辑器
*/
public class UtilDatePropertyEditor extends PropertyEditorSupport{
private String pattern;//日期时间格式
public void setPattern(String pattern) {
this.pattern = pattern;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
try {
Date d=new SimpleDateFormat(pattern).parse(text);
this.setValue(d);//设置转换后的值
} catch (ParseException e) {
e.printStackTrace();
}
}
}
注意要继承PropertyEditorSupport类并实现setAsText方法。
转换器的配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="customEditors"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<!-- 属性编辑器要放到 CustomEditorConfigurer 类的 customEditors(Map类型)成员变量上面去 -->
<property name="customEditors">
<map>
<entry key="java.util.Date">
<!-- 内部bean,只内部使用 -->
<bean class="com.shuitian.spring.UtilDatePropertyEditor">
<!-- 注入日期时间格式 -->
<property name="pattern" value="yyyy-MM-dd"/>
</bean>
</entry>
</map>
</property>
</bean>
<!-- 不想通过外界访问到 -->
<!-- <bean id="utilDatePropertyEditor" class="com.shuitian.spring.UtilDatePropertyEditor">
</bean> -->
</beans>
add进spring的源码,围观下:
这一配置的原因就是,我们要将自己定义的属性编辑器,放到CustomEditorConfigurer 它的customEditors里面,这样spring才能使用到它。