参见附件代码理解下文(附件中有更加详细的注解)
一、普通的属性注入
1)bean类
package com.bjsxt.spring; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; public class Bean1 { private String strValue; private int intValue; private List listValue; private Set setValue; private String[] arrayValue; private Map mapValue; private Date dateValue; public String getStrValue() { return strValue; } public void setStrValue(String strValue) { this.strValue = strValue; } public int getIntValue() { return intValue; } public void setIntValue(int intValue) { this.intValue = intValue; } 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 String[] getArrayValue() { return arrayValue; } public void setArrayValue(String[] arrayValue) { this.arrayValue = arrayValue; } 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; } }
2)相应的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="bean1" class="com.bjsxt.spring.Bean1"> <property name="strValue" value="Hello"/> <!-- <property name="intValue" value="123"/> --> <property name="intValue"> <value>123</value> </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="arrayValue"> <list> <value>array1</value> <value>array2</value> </list> </property> <property name="mapValue"> <map> <entry key="k1" value="v1"/> <entry key="k2" value="v2"/> </map> </property> <property name="dateValue"> <value>2008-08-15</value> </property> </bean> <bean id="bean2" class="com.bjsxt.spring.Bean2"> <property name="bean3" ref="bean3"/> <property name="bean4"> <ref bean="bean4"/> </property> <property name="bean5" ref="bean5"/> </bean> <!-- <bean id="bean3" class="com.bjsxt.spring.Bean3"> <property name="id" value="1000"/> <property name="name"> <value>Jack</value> </property> <property name="password" value="123"/> </bean> <bean id="bean4" class="com.bjsxt.spring.Bean4"> <property name="id" value="1000"/> <property name="name" value="Jack"/> </bean> --> <bean id="bean5" class="com.bjsxt.spring.Bean5"> <property name="age" value="20"/> </bean> </beans>
3)相应的测试代码
package com.bjsxt.spring; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import junit.framework.TestCase; public class InjectionTest extends TestCase { private BeanFactory factory; @Override protected void setUp() throws Exception { factory = new ClassPathXmlApplicationContext("applicationContext-*.xml"); } public void testInjection1() { Bean1 bean1 = (Bean1)factory.getBean("bean1"); System.out.println("bean1.strValue=" + bean1.getStrValue()); System.out.println("bean1.intValue=" + bean1.getIntValue()); System.out.println("bean1.listValue=" + bean1.getListValue()); System.out.println("bean1.setValue=" + bean1.getSetValue()); System.out.println("bean1.arrayValue=" + bean1.getArrayValue()); System.out.println("bean1.mapValue=" + bean1.getMapValue()); System.out.println("bean1.dateValue=" + bean1.getDateValue()); }}
spring的普通属性注入
参见:spring文档3.3章节
二、什么是属性编辑器,作用?
如上例中如果注解Date属性,Spring就会报错,那么怎么办呢。Spring给我们提供了属性编辑器相当于Struts的convert方法,对注入的属性可以进行转换。
* 自定义属性编辑器,当spring遇到需要转换的类型时,该类型一般就是applicationContext-editor.xml中的Key所表示的类型(如本例中的<entry key="java.util.Date">),那么当在spring会自动将注入的字符串转换成相应的需要转换的对象进行注入
spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器类
* 如何定义属性编辑器?
* 1、继承PropertyEditorSupport类,覆写setAsText()方法,编写自己写的UtilDatePropertyEditor.java
类。参见:UtilDatePropertyEditor.java
package com.bjsxt.spring; import java.beans.PropertyEditorSupport; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * java.util.Date属性编辑器 * @author Administrator * */ public class UtilDatePropertyEditor extends PropertyEditorSupport { private String format="yyyy-MM-dd"; @Override public void setAsText(String text) throws IllegalArgumentException { System.out.println("UtilDatePropertyEditor.saveAsText() -- 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; } }
* 2、将UtilDatePropertyEditor.java属性编辑器注册到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"> <!-- 定义属性编辑器 --> <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date"> <bean class="com.bjsxt.spring.UtilDatePropertyEditor"> <property name="format" value="yyyy-MM-dd"/> </bean> </entry> </map> </property> </bean> <!-- <bean id="utilDatePropertyEditor" class="com.bjsxt.spring.UtilDatePropertyEditor"></bean> --> </beans>
三、公共的注入
依赖对象的注入方式,可以采用:
* ref属性
* <ref>标签
* 内部<bean>来定义
如何将公共的注入定义描述出来?
* 通过<bean>标签定义公共的属性,指定abstract=true
* 具有相同属性的类在<bean>标签中指定其parent属性
参见:applicationContext-other.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="beanAbstract" abstract="true"> <property name="id" value="1000"/> <property name="name" value="Jack"/> </bean> <bean id="bean3" class="com.bjsxt.spring.Bean3" parent="beanAbstract"> <property name="name" value="Tom"/> <property name="password" value="123"/> </bean> <bean id="bean4" class="com.bjsxt.spring.Bean4" parent="beanAbstract"/> </beans>
测试代码
package com.bjsxt.spring; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import junit.framework.TestCase; public class InjectionTest extends TestCase { private BeanFactory factory; @Override protected void setUp() throws Exception { factory = new ClassPathXmlApplicationContext("applicationContext-*.xml"); } public void testInjection2() { Bean2 bean2 = (Bean2)factory.getBean("bean2"); System.out.println("bean2.bean3.id=" + bean2.getBean3().getId()); System.out.println("bean2.bean3.name=" + bean2.getBean3().getName()); System.out.println("bean2.bean3.password=" + bean2.getBean3().getPassword()); System.out.println("bean2.bean4.id=" + bean2.getBean4().getId()); System.out.println("bean2.bean4.name=" + bean2.getBean4().getName()); System.out.println("bean2.bean5.age=" + bean2.getBean5().getAge()); } }