Spring Bean基本管理实例详解
本文实例讲述了SpringBean基本管理。分享给大家供大家参考,具体如下:
一、使用setter方式完成依赖注入
下面是Bean和beans-config.xml文件。
publicclassHelloBean{ privateStringhelloWord; //...省略getter、setter方法 }
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEbeansPUBLIC"-//SPRING/DTDBEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <beanid="helloBean" class="onlyfun.caterpillar.HelloBean"> <propertyname="helloWord"> <value>Hello!Justin!</value> </property> </bean> </beans>
publicclassSpringDemo{ publicstaticvoidmain(String[]args){ Resourcers=newFileSystemResource("beans-config.xml"); BeanFactoryfactory=newXmlBeanFactory(rs); HelloBeanhello=(HelloBean)factory.getBean("helloBean"); System.out.println(hello.getHelloWord()); } }
二、使用constructor方式完成注入
publicclassHelloBean{ privateStringname; privateStringhelloWord; //建议有要无参数建构方法 publicHelloBean(){ } publicHelloBean(Stringname,StringhelloWord){ this.name=name; this.helloWord=helloWord; } //...省略getter、setter方法 }
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEbeansPUBLIC"-//SPRING/DTDBEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <beanid="helloBean" class="onlyfun.caterpillar.HelloBean"> <constructor-argindex="0"> <value>Justin</value> </constructor-arg> <constructor-argindex="1"> <value>Hello</value> </constructor-arg> </bean> </beans>
publicclassSpringDemo{ publicstaticvoidmain(String[]args){ ApplicationContextcontext= newFileSystemXmlApplicationContext("beans-config.xml"); HelloBeanhello=(HelloBean)context.getBean("helloBean"); System.out.print("Name:"); System.out.println(hello.getName()); System.out.print("Word:"); System.out.println(hello.getHelloWord()); } }
三、属性参考
publicclassHelloBean{ privateStringhelloWord; privateDatedate; //...省略getter、setter方法 }
<beans> <beanid="dateBean"class="java.util.Date"/> <beanid="helloBean"class="onlyfun.caterpillar.HelloBean"> <propertyname="helloWord"> <value>Hello!</value> </property> <propertyname="date"> <refbean="dateBean"/> </property> </bean> </beans>
publicclassSpringDemo{ publicstaticvoidmain(String[]args){ ApplicationContextcontext= newFileSystemXmlApplicationContext("beans-config.xml"); HelloBeanhello=(HelloBean)context.getBean("helloBean"); System.out.print(hello.getHelloWord()); System.out.print("It's"); System.out.print(hello.getDate()); System.out.println("."); } }
四、“byType”自动绑定
将“三”中的配置文件改为下面,即可完成bean属性的按类型自动绑定。
<beans> <beanid="dateBean"class="java.util.Date"/> <beanid="helloBean"class="onlyfun.caterpillar.HelloBean"autowire="byType"> <propertyname="helloWord"> <value>Hello!</value> </property> </bean> </beans>
五、“byName”自动绑定
将“三”中的配置文件改为下面,即可完成bean属性的按名称自动绑定。
<beans> <beanid="dateBean"class="java.util.Date"/> <beanid="helloBean"class="onlyfun.caterpillar.HelloBean"autowire="byName"> <propertyname="helloWord"> <value>Hello!</value> </property> </bean> </beans>
六、“constructor”自动绑定
将“三”中的配置文件改为下面,即可完成bean属性的按构造方法自动绑定。在建立依赖关系时,Srping容器会试图比对容器中的Bean实例类型,及相关的构造方法上的参数类型,看看在类型上是否符合,如果有的话,则选用该构造方法来建立Bean实例。如果无法绑定,则抛出org.springframework.beans.factory.UnsatisfiedDependencyException异常。
<beans> <beanid="dateBean"class="java.util.Date"/> <beanid="helloBean"class="onlyfun.caterpillar.HelloBean"autowire="constructor"> <propertyname="helloWord"> <value>Hello!</value> </property> </bean> </beans>
六、“autodetect”自动绑定
将“三”中的配置文件改为下面,即可完成bean属性的自动绑定,这个自动绑定是Spring会尝试用入constructor来处理依赖关系的建立,如果不行,则再尝试用byType类建立依赖关系。
<beans> <beanid="dateBean"class="java.util.Date"/> <beanid="helloBean"class="onlyfun.caterpillar.HelloBean"autowire="autodetect"> <propertyname="helloWord"> <value>Hello!</value> </property> </bean> </beans>
七、依赖检查方式
在自动绑定中,由于没办法从定义文件中,清楚地看到是否每个属性都完成设定,为了确定某些依赖关系确实建立,您可以假如依赖检查,在<bean>标签使用时设定"dependency-check",可以有四种依赖检查方式:simple、objects、all、none。
simple:只检查简单的类型(像原生数据类型或字符串对象)属性是否完成依赖关系,。
objects:检查对象类型的属性是否完成依赖关系。
all:则检查全部的属性是否完成依赖关系。
none:设定是默认值,表示不检查依赖性。
<beans> <beanid="dateBean"class="java.util.Date"/> <beanid="helloBean"class="onlyfun.caterpillar.HelloBean"autowire="autodetect"dependeny-check="all"> <propertyname="helloWord"> <value>Hello!</value> </property> </bean> </beans>
八、集合对象注入
对于像数组、List、Set、Map等集合对象,在注入前必须填充一些对象至集合中,然后再将集合对象注入至所需的Bean时,也可以交由Spring的IoC容器来自动维护或生成集合对象,并完成依赖注入。
publicclassSomeBean{ privateString[]someStrArray; privateSome[]someObjArray; privateListsomeList; privateMapsomeMap; publicString[]getSomeStrArray(){ returnsomeStrArray; } publicvoidsetSomeStrArray(String[]someStrArray){ this.someStrArray=someStrArray; } publicSome[]getSomeObjArray(){ returnsomeObjArray; } publicvoidsetSomeObjArray(Some[]someObjArray){ this.someObjArray=someObjArray; } publicListgetSomeList(){ returnsomeList; } publicvoidsetSomeList(ListsomeList){ this.someList=someList; } publicMapgetSomeMap(){ returnsomeMap; } publicvoidsetSomeMap(MapsomeMap){ this.someMap=someMap; } } publicclassSome{ privateStringname; publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicStringtoString(){ returnname; } }
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEbeansPUBLIC"-//SPRING/DTDBEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <beanid="some1"class="onlyfun.caterpillar.Some"> <propertyname="name"> <value>Justin</value> </property> </bean> <beanid="some2"class="onlyfun.caterpillar.Some"> <propertyname="name"> <value>momor</value> </property> </bean> <beanid="someBean"class="onlyfun.caterpillar.SomeBean"> <propertyname="someStrArray"> <list> <value>Hello</value> <value>Welcome</value> </list> </property> <propertyname="someObjArray"> <list> <refbean="some1"/> <refbean="some2"/> </list> </property> <propertyname="someList"> <list> <value>ListTest</value> <refbean="some1"/> <refbean="some2"/> </list> </property> <propertyname="someMap"> <map> <entrykey="MapTest"> <value>Hello!Justin!</value> </entry> <entrykey="someKey1"> <refbean="some1"/> </entry> </map> </property> </bean> </beans>
publicclassSpringDemo{ publicstaticvoidmain(String[]args){ ApplicationContextcontext= newFileSystemXmlApplicationContext( "beans-config.xml"); SomeBeansomeBean= (SomeBean)context.getBean("someBean"); //取得数组型态依赖注入对象 String[]strs= (String[])someBean.getSomeStrArray(); Some[]somes= (Some[])someBean.getSomeObjArray(); for(inti=0;i<strs.length;i++){ System.out.println(strs[i]+"," +somes[i].getName()); } //取得List型态依赖注入对象 System.out.println(); ListsomeList=(List)someBean.getSomeList(); for(inti=0;i<someList.size();i++){ System.out.println(someList.get(i)); } //取得Map型态依赖注入对象 System.out.println(); MapsomeMap=(Map)someBean.getSomeMap(); System.out.println(someMap.get("MapTest")); System.out.println(someMap.get("someKey1")); } }
希望本文所述对大家Java程序设计有所帮助。