Spring常用属性的注入及属性编辑器
来源:互联网 发布:windows可上网镜像 编辑:程序博客网 时间:2024/06/08 16:12
对于对象的注入,我们使用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;}@Overridepublic 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
- Spring常用属性的注入及属性编辑器
- Spring注入Date类型的属性,自定义Date属性编辑器
- Spring属性注入(普通属性注入,自定义属性编辑器)
- Spring---->属性的注入
- 【Spring 基础篇三】属性注入与属性编辑器
- Spring属性注入Bean的List属性
- 2.Spring bean常见属性的注入:属性编辑器,String->java.util.Date
- 关于spring的属性注入
- Spring属性注入的方式
- spring属性的注入实例
- Spring的属性注入(SPEL)
- spring 静态属性的注入
- spring 常用属性注入和自动装配
- 【Spring 2】spring的属性注入形式
- 对象Date属性的注入(属性编辑器)
- spring 属性注入
- spring公共属性注入
- Spring中的属性注入
- Spring mvc——接收对象Json数据,<mvc:annotation-driven/><context:annotation-config/><context:component-scan/>
- C#反射是什么
- JqMobi 基础知识
- 自动更新工具类
- Tomcat--系统架构与设计模式,第 1 部分: 工作原理
- Spring常用属性的注入及属性编辑器
- Java初涉之6--浅谈反射
- 十二月英语学习总结-希冀
- 判断网络是否可用
- LeetCode 257:Binary Tree Paths
- 报错: org.apache.commons.pool2.impl.GenericObjectPoolConfig not found
- java网络协议(IPV4/IPV6)
- Masonry介绍与使用实践:快速上手Autolayout
- 扩展方法