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
- IOC容器之灵活配置对象属性值一
- IOC容器之灵活配置对象属性值二->Date
- IOC容器之灵活配置对象属性值三->抽象
- Spring之IoC容器解析(一)
- JAVAEE之Spring学习(一)-----ioc容器
- IOC容器类对象
- Spring--IOC容器 《一》
- Spring IoC容器(一)
- Castle IOC容器构建配置详解(一)
- Spring的核心之IoC容器创建对象
- Spring之IoC容器
- spring之ioc容器
- Spring之IOC容器
- Castle IOC容器实践之Startable Facility(一)
- Castle IOC容器实践之TypedFactory Facility(一)
- 跟我动手搭框架一之IOC容器实现
- Castle IOC容器与Spring.NET配置之比较
- Castle IOC容器与Spring.NET配置之比较
- jQuery-使用jQuery进行Dom操作
- 【BLE】CC2541之发现多个特征值句柄
- 1094. The Largest Generation (25)
- 在android中批量执行sqlite的sql脚本
- kali linux 升级至2.0
- IOC容器之灵活配置对象属性值一
- CloudantDB创建view(In fact,it is the secondary Indexes)
- PHP heredoc技术
- Uva 177 Paper Folding(模拟?Orz)
- Linux查看内核(Kernel)版本的方法
- Mac虚拟机安装win7教程之前奏曲
- 改变导航栏的背景,导航栏上的文字的颜色,按钮的颜色
- OOA和OOD
- POJ 1724ROADS(费用最短路:优先队列+BFS)