Spring经常使用属性的注入及属性编辑器


      

           对于对象的注入,我们使用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才干使用到它。









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值