前面的示例1展示了怎样配置运行一个简单地Spring项目,下面再来继续实现在配置文件applicationContext.xml中给参数注入值。
1.程序结构图:(将程序所需要的Spring相关jar包拷贝到程序的 lib 目录下)。
2.程序Bean1的代码(Bean1.java)
package com.my_spring;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Bean1 {
//下面是多种类型数据的注入测试
private int intValue;
private String strValue;
private String[] arrayValue;
private List listValue;
private Set setValue;
private Map mapValue;
private Date dateValue;
public int getIntValue() {
return intValue;
}
public void setIntValue(int intValue) {
this.intValue = intValue;
}
public String getStrValue() {
return strValue;
}
public void setStrValue(String strValue) {
this.strValue = strValue;
}
public String[] getArrayValue() {
return arrayValue;
}
public void setArrayValue(String[] arrayValue) {
this.arrayValue = arrayValue;
}
public List getListValue() {
return listValue;
}
public void setListValue(List listValue) {
this.listValue = listValue;
}
public Set getSetValue() {
return setValue;
}
public void setSetValue(Set setValue) {
this.setValue = setValue;
}
public Map getMapValue() {
return mapValue;
}
public void setMapValue(Map mapValue) {
this.mapValue = mapValue;
}
public Date getDateValue() {
return dateValue;
}
public void setDateValue(Date dateValue) {
this.dateValue = dateValue;
}
}3.自定义Date的属性编辑器(UtilDatePropertyEditor.java)
package com.my_spring;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//java.util.Date属性编辑器
public class UtilDatePropertyEditor extends PropertyEditorSupport {
private String format = "yyyy-mm-dd";
@Override
public void setAsText(String text) throws IllegalArgumentException {
System.out.println("UtilDatePropertyEditor.setAsText() -- text = " + text);
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date d = sdf.parse(text);
this.setValue(d);
} catch (ParseException e) {
e.printStackTrace();
}
}
public void setFormat(String format) {
this.format = format;
}
}
4.利用JUint来进行的测试类代码(InjectionTest.java)
package com.my_spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;
public class InjectionTest extends TestCase {
BeanFactory factory;
@Override
protected void setUp() throws Exception {
factory = new ClassPathXmlApplicationContext("applicationContext.xml");
}
public void testInjection() {
Bean1 bean1 = (Bean1)factory.getBean("bean1");
System.out.println("bean1.intValue=" + bean1.getIntValue());
System.out.println("bean1.strValue=" + bean1.getStrValue());
System.out.println("bean1.arrayValue=" + bean1.getArrayValue());
System.out.println("bean1.listValue=" + bean1.getListValue());
System.out.println("bean1.setValue=" + bean1.getSetValue());
System.out.println("bean1.mapValue=" + bean1.getMapValue());
System.out.println("bean1.dateValue=" + bean1.getDateValue());
}
}
5.Bean的配置文件(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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<bean id="bean1" class="com.my_spring.Bean1">
<!-- 下为设置注入的方法 -->
<property name="intValue" value="123" />
<!-- 也可以使用下面这种方式 -->
<property name="strValue">
<value>木星人你好!</value>
</property>
<property name="arrayValue">
<list>
<value>array1</value>
<value>array2</value>
</list>
</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="mapValue">
<map>
<entry key="k1" value="v1" />
<entry key="k2" value="v2" />
</map>
</property>
<!--将bean1中的Date赋值2014-05-07,spring会认为2014-05-07是String,无法转换成Date,会报错!-->
<property name="dateValue">
<value>2014-05-07</value>
</property>
</bean>
<!-- 于是定义属性编辑器 -->
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.my_spring.UtilDatePropertyEditor">
<property name="format" value="yyyy-mm-dd" />
</bean>
</entry>
</map>
</property>
</bean>
</beans>
6.运行结果截图
下面提供个Spring framework下载地址
点击打开Spring下载链接
本文介绍了一个简单的Spring项目配置,并详细展示了如何在配置文件applicationContext.xml中进行参数注入,包括基本数据类型、集合类型的注入及自定义日期编辑器的实现。
693

被折叠的 条评论
为什么被折叠?



