Spring Bean基本管理实例详解
本文实例讲述了spring bean基本管理。分享给大家供大家参考,具体如下:
一、使用setter方式完成依赖注入
下面是bean和beans-config.xml文件。
public class hellobean { private string helloword; //...省略getter、setter方法 }
<?xml version="1.0" encoding="utf-8"?> <!doctype beans public "-//spring/dtd bean/en" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="hellobean" class="onlyfun.caterpillar.hellobean"> <property name="helloword"> <value>hello!justin!</value> </property> </bean> </beans>
public class springdemo { public static void main(string[] args) { resource rs = new filesystemresource("beans-config.xml"); beanfactory factory = new xmlbeanfactory(rs); hellobean hello = (hellobean) factory.getbean("hellobean"); system.out.println(hello.gethelloword()); } }
二、使用constructor方式完成注入
public class hellobean { private string name; private string helloword; // 建议有要无参数建构方法 public hellobean() { } public hellobean(string name, string helloword) { this.name = name; this.helloword = helloword; } //...省略getter、setter方法 }
<?xml version="1.0" encoding="utf-8"?> <!doctype beans public "-//spring/dtd bean/en" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="hellobean" class="onlyfun.caterpillar.hellobean"> <constructor-arg index="0"> <value>justin</value> </constructor-arg> <constructor-arg index="1"> <value>hello</value> </constructor-arg> </bean> </beans>
public class springdemo { public static void main(string[] args) { applicationcontext context = new filesystemxmlapplicationcontext("beans-config.xml"); hellobean hello = (hellobean) context.getbean("hellobean"); system.out.print("name: "); system.out.println(hello.getname()); system.out.print("word: "); system.out.println(hello.gethelloword()); } }
三、属性参考
public class hellobean { private string helloword; private date date; //...省略getter、setter方法 }
<beans> <bean id="datebean" class="java.util.date"/> <bean id="hellobean" class="onlyfun.caterpillar.hellobean"> <property name="helloword"> <value>hello!</value> </property> <property name="date"> <ref bean="datebean"/> </property> </bean> </beans>
public class springdemo { public static void main(string[] args) { applicationcontext context = new filesystemxmlapplicationcontext("beans-config.xml"); hellobean hello = (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> <bean id="datebean" class="java.util.date"/> <bean id="hellobean" class="onlyfun.caterpillar.hellobean" autowire="bytype"> <property name="helloword"> <value>hello!</value> </property> </bean> </beans>
五、“byname”自动绑定
将“三”中的配置文件改为下面,即可完成bean属性的按名称自动绑定。
<beans> <bean id="datebean" class="java.util.date"/> <bean id="hellobean" class="onlyfun.caterpillar.hellobean" autowire="byname"> <property name="helloword"> <value>hello!</value> </property> </bean> </beans>
六、“constructor”自动绑定
将“三”中的配置文件改为下面,即可完成bean属性的按构造方法自动绑定。在建立依赖关系时,srping容器会试图比对容器中的bean实例类型,及相关的构造方法上的参数类型,看看在类型上是否符合,如果有的话,则选用该构造方法来建立bean实例。如果无法绑定,则抛出org.springframework.beans.factory.unsatisfieddependencyexception异常。
<beans> <bean id="datebean" class="java.util.date"/> <bean id="hellobean" class="onlyfun.caterpillar.hellobean" autowire="constructor"> <property name="helloword"> <value>hello!</value> </property> </bean> </beans>
六、“autodetect”自动绑定
将“三”中的配置文件改为下面,即可完成bean属性的自动绑定,这个自动绑定是spring会尝试用入constructor来处理依赖关系的建立,如果不行,则再尝试用bytype类建立依赖关系。
<beans> <bean id="datebean" class="java.util.date"/> <bean id="hellobean" class="onlyfun.caterpillar.hellobean" autowire="autodetect"> <property name="helloword"> <value>hello!</value> </property> </bean> </beans>
七、依赖检查方式
在自动绑定中,由于没办法从定义文件中,清楚地看到是否每个属性都完成设定,为了确定某些依赖关系确实建立,您可以假如依赖检查,在<bean>标签使用时设定"dependency-check",可以有四种依赖检查方式:simple、objects、all、none。
simple:只检查简单的类型(像原生数据类型或字符串对象)属性是否完成依赖关系,。
objects:检查对象类型的属性是否完成依赖关系。
all:则检查全部的属性是否完成依赖关系。
none:设定是默认值,表示不检查依赖性。
<beans> <bean id="datebean" class="java.util.date"/> <bean id="hellobean" class="onlyfun.caterpillar.hellobean" autowire="autodetect" dependeny-check="all"> <property name="helloword"> <value>hello!</value> </property> </bean> </beans>
八、集合对象注入
对于像数组、list、set、map等集合对象,在注入前必须填充一些对象至集合中,然后再将集合对象注入至所需的bean时,也可以交由spring的ioc容器来自动维护或生成集合对象,并完成依赖注入。
public class somebean { private string[] somestrarray; private some[] someobjarray; private list somelist; private map somemap; public string[] getsomestrarray() { return somestrarray; } public void setsomestrarray(string[] somestrarray) { this.somestrarray = somestrarray; } public some[] getsomeobjarray() { return someobjarray; } public void setsomeobjarray(some[] someobjarray) { this.someobjarray = someobjarray; } public list getsomelist() { return somelist; } public void setsomelist(list somelist) { this.somelist = somelist; } public map getsomemap() { return somemap; } public void setsomemap(map somemap) { this.somemap = somemap; } } public class some { private string name; public string getname() { return name; } public void setname(string name) { this.name = name; } public string tostring() { return name; } }
<?xml version="1.0" encoding="utf-8"?> <!doctype beans public "-//spring/dtd bean/en" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="some1" class="onlyfun.caterpillar.some"> <property name="name"> <value>justin</value> </property> </bean> <bean id="some2" class="onlyfun.caterpillar.some"> <property name="name"> <value>momor</value> </property> </bean> <bean id="somebean" class="onlyfun.caterpillar.somebean"> <property name="somestrarray"> <list> <value>hello</value> <value>welcome</value> </list> </property> <property name="someobjarray"> <list> <ref bean="some1"/> <ref bean="some2"/> </list> </property> <property name="somelist"> <list> <value>listtest</value> <ref bean="some1"/> <ref bean="some2"/> </list> </property> <property name="somemap"> <map> <entry key="maptest"> <value>hello!justin!</value> </entry> <entry key="somekey1"> <ref bean="some1"/> </entry> </map> </property> </bean> </beans>
public class springdemo { public static void main(string[] args) { applicationcontext context = new filesystemxmlapplicationcontext( "beans-config.xml"); somebean somebean = (somebean) context.getbean("somebean"); // 取得数组型态依赖注入对象 string[] strs = (string[]) somebean.getsomestrarray(); some[] somes = (some[]) somebean.getsomeobjarray(); for(int i = 0; i < strs.length; i++) { system.out.println(strs[i] + "," + somes[i].getname()); } // 取得list型态依赖注入对象 system.out.println(); list somelist = (list) somebean.getsomelist(); for(int i = 0; i < somelist.size(); i++) { system.out.println(somelist.get(i)); } // 取得map型态依赖注入对象 system.out.println(); map somemap = (map) somebean.getsomemap(); system.out.println(somemap.get("maptest")); system.out.println(somemap.get("somekey1")); } }
希望本文所述对大家java程序设计有所帮助。