【Spring 基础篇三】属性注入与属性编辑器

     上篇我们了解了一下applicationContext.xml的两种注入方式,本篇我们来了解一下关于属性的注入以及操作。

     在敲代码的过程中,我们很容易遇到这样的问题,比如一个List的集合,我之前给他赋值两个,现在我想给他再重新赋值,一般的想法当然是修改代码,然后完成自己想要的需求了,但是这种做法是不可取了,所以我们完成可以采用Spring属性注入的方式来解决此问题。看如下demo:

1、我们创建一个类,来创建几个不同类型的属性:

常用属性的注入有:int,String,list,set,map等。

Bean01属性类:

public class Bean01 {

	private String strValue;  //String类型实体

	private int intValue;   //int类型实体<span style="font-family: Arial, Helvetica, sans-serif;">  </span>

	private List listValue;   //list类型实体

	private Set setValue;    //Set类型实体

	private String[] arrayValue;  //String集合实体

	private Map mapValue;   //Map集合实体

	private Date dateValue;   //date类型实体

	…………省略get,set方法
}
     由于有了set方法,所以我们完全可以采用setter注入的方式对其属性赋值

applicationContext.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" 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="bean01" class="com.bjpowernode.spring.Bean01">
		<property name="strValue" value="Hello_Spring" />
		<property name="intValue" value="123" />
		<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">
			<set>
				<value>array1</value>
				<value>array2</value>
			</set>
		</property>
		<property name="mapValue">
			<map>
				<entry key="k1" value="v1"></entry>
				<entry key="k2" value="v2"></entry>
			</map>
		</property>
		<property name="dateValue" value="2011-10-21">  //日期格式赋值
		</property>
	</bean>
</beans>

     但是有好多信息并非是简简单单的赋值就能够满足需求的,比如我们的日期,存有各种格式,到界面会存在乱码或者格式显示的问题,相关日期乱码博客Laydate日期乱码。我们该如何去应如自如呢?当然,对于这种情况,我们完全可以单独罗列出来,新建一个类来完成我们的需求,对于这个类的作用,我们称其为“属性编辑器”。

如何自定义属性编辑器?
  • 继承PropertyEditorSupport
  • 覆盖setAsText()方法
  • 将自定义的属性编辑器注入到spring中

创建UtilDatePropertyEditor类:

// 继承于Java beans的PropertyEditorSupport来实现我们的功能。
public class UtilDatePropertyEditor extends PropertyEditorSupport {

	// 定义一个实体,获得set方法
	private String pattern;

	// 获得的set方法,便于注入
	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	// 覆盖方法setAsText满足需求
	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		// 输出来,便于测试显示结果
		System.out.println("---UtilDatePropertyEditor.setAsText()---" + text);
		try {
			// 只适用于一种格式
			Date date = new SimpleDateFormat(pattern).parse(text);
			// 将其赋值进去
			this.setValue(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// 异常抛出
			throw new IllegalArgumentException(text);
		}
	}
}
     由于Spring是支持多文件配置的,就和文件一样,每个文件都干着不一样的事,所以我们可以再新建一个配置applicationContext-editor.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" 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">
	<!-- 注:ID不能和之前的配置文件重复 --><!-- 将属性编辑器注入给Spring,Class为CustomEditorConfigurer的路径 -->
	<bean id="customEditors"
		class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<!--将属性编辑器放进CustomEditorConfigurer的成员变量customEditors中进行保存,通过setter方式注入  -->
		<property name="customEditors">
			<map>
			<!--key值为要转换的类型  -->
				<entry key="java.util.Date">
					<!--内部转换器 -->
					<bean class="com.bjpowernode.spring.UtilDatePropertyEditor">
						<!--通过setter的方式注入对pattern(多个日期的选择)的设置 -->
						<property name="pattern" value="yyyy-MM-dd"></property>
					</bean>
				</entry>
			</map>
		</property>
	</bean>

	<!--转换器 -->
	<!--注:这个转换器到底是私有还是共有,放在外边则是大家都可以使用,我们可以选择放在里边 -->
	<!-- <bean id="utilDatePropertyEditor" class="com.bjpowernode.spring.UtilDatePropertyEditor"></bean> -->
</beans>
     这样我们的关于日期的一个属性编辑器就完工了,不管是pattern,日期类型的注入还是整个编辑器的注入,整个过程都采用的setter的注入方式,最后我们使用单元测试的方式测试一下:

public class InjectionTest extends TestCase {

	// 定义成员变量
	private BeanFactory factory;

	@Override
	protected void setUp() throws Exception {

		// 只创建一次对配置文件的读取
		/*
		 * factory = new
		 * ClassPathXmlApplicationContext("applicationContext.xml");
		 */

		// 传递数组,两个配置文件的读取
		String[] configLocations = new String[] { "applicationContext.xml",
				"applicationContext-editor.xml" };
		factory = new ClassPathXmlApplicationContext(configLocations);
	}

	@Override
	protected void tearDown() throws Exception {
		// 销毁,删除的方法
	}

	public void testInjection1() {
		Bean01 bean01 = (Bean01) factory.getBean("bean01");
		System.out.println("bean01.strValue=" + bean01.getStrValue());
		System.out.println("bean01.intValue=" + bean01.getIntValue());
		System.out.println("bean01.listValue=" + bean01.getListValue());
		System.out.println("bean01.setValue=" + bean01.getSetValue());
		System.out.println("bean01.arrayValue=" + bean01.getArrayValue());
		System.out.println("bean01.mapValue=" + bean01.getMapValue());
		System.out.println("bean01.dateValue=" + bean01.getDateValue());
	}
最后的结果:


总结:

     1、通过spring的BeanFactory实例化,配置和管理对象,当有多个对象的时候,其实除了数组的方式来管理,我们还可以采取规范化的管理方式,在配置文件定义名字的时候,我们可以规定为统一的格式,比如上述例子的两个文件,我们可以修改名称为:“applicationContext-beans.xml”、“applicationContext-editor.xml”

这样我们则可以简单化调用:

factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值