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

大家都知道,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;
//相当于初始化,只执行一次        
@Override
protectedvoid setUp() throws Exception {
beanFactory=newClassPathXmlApplicationContext("applicationContext.xml");
}
@Override
protectedvoid 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值