IOC容器之灵活配置对象属性值一

来源:互联网 发布:深圳网络教育报名 编辑:程序博客网 时间:2024/05/02 02:21

大家都知道,IOC容器核心就是创建对象及维护对象间的引用。下面我们来说一说它是如何给给对象的属性灵活配置的。

java类:


publicclass Bean1 {privateStringstrValue;                privateint intValue;privateList listValue;privateSet setValue;privateString[] arrayValues;privateMap mapValue;  publicvoid setStrValue(String strValue) {this.strValue= strValue;} publicvoid setIntValue(int intValue) {this.intValue= intValue;} publicvoid setListValue(List listValue) {this.listValue= listValue;} publicvoid setSetValue(Set setValue) {this.setValue= setValue;} publicvoid setMapValue(Map mapValue) {this.mapValue= mapValue;} } 
这里一定要注意,set属性一定要设置,因为IOC需要通过set方式注入进去。

applicationContext-beans.xml:

<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd           http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <beanid="bean1" class="com.huxj.spring.Bean1"> <propertyname="dateValue" value="2015/09/06" />  <propertyname="strValue" value="huxiaojie" /><propertyname="intValue" value="123" /> <propertyname="listValue"><list><value>list1</value><value>list2</value></list></property> <propertyname="setValue"><set><value>set1</value><value>set2</value></set></property> <propertyname="arrayValues"><list><value>arrayList1</value><value>arrayList2</value></list></property> <propertyname="mapValue"><map><entrykey="k1" value="v1" /><entrykey="k2" value="v2" /></map></property>  </bean></beans>

通过<bean>下的<porperty>配置节进行属性的配置。

注意:读取spring.xml文件的所有数据类型都是string类型,但是spring本身提供了常用数据类型(string,int,String[])自动转化的机制,所以对于类型转换我就不用操心了。

当然也有一些特殊情况。

测试用例

packagetest; importorg.springframework.beans.factory.BeanFactory;importorg.springframework.context.support.ClassPathXmlApplicationContext; importcom.huxj.spring.Bean1;importcom.huxj.spring.Bean2; importjunit.framework.TestCase; publicclass InjectionTest extends TestCase { BeanFactorybeanFactory;//相当于初始化,只执行一次        @Overrideprotectedvoid setUp() throws Exception {beanFactory=newClassPathXmlApplicationContext("applicationContext.xml");}@Overrideprotectedvoid tearDown() throws Exception{                        }         //测试属性注入方法publicvoid testInjection1(){Bean1bean1=(Bean1)beanFactory.getBean("bean1");System.out.print("bean1.strValue="+bean1.getStrValue()+"\n");System.out.print("bean1.intValue="+bean1.getIntValue()+"\n");System.out.print("bean1.listValue="+bean1.getListValue()+"\n");System.out.print("bean1.setValue="+bean1.getSetValue()+"\n");System.out.print("bean1.arrayValues="+bean1.getArrayValues()+"\n");System.out.print("bean1.mapValue="+bean1.getMapValue()+"\n");System.out.print("bean1.dateValue="+bean1.getDateValue()+"\n");}}


结果:




总结:

由于spring文档解析出之后数据类型都是String类型的,当然类型的转换工作spring基本也给我们处理好了。上面也提到了,也有一些特殊情况比如(Date等),那我们应该怎么做得,下篇博客将会介绍。

0 0
原创粉丝点击