首页 > 开发 > Java > 正文

Spring Bean基本管理实例详解

2024-07-13 09:55:54
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Spring Bean基本管理,以实例形式较为详细的分析了Spring Bean的相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Spring Bean基本管理。分享给大家供大家参考,具体如下:

一、使用setter方式完成依赖注入

下面是Bean和beans-config.xml文件。

 

 
  1. public class HelloBean {  
  2. private String helloWord;  
  3. //...省略getter、setter方法  

 

 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"  
  3. "http://www.springframework.org/dtd/spring-beans.dtd">  
  4. <beans>  
  5. <bean id="helloBean" 
  6. class="onlyfun.caterpillar.HelloBean">  
  7. <property name="helloWord">  
  8. <value>Hello!Justin!</value>  
  9. </property>  
  10. </bean>  
  11. </beans> 

 

 
  1. public class SpringDemo {  
  2. public static void main(String[] args) {  
  3. Resource rs = new FileSystemResource("beans-config.xml");  
  4. BeanFactory factory = new XmlBeanFactory(rs);  
  5. HelloBean hello = (HelloBean) factory.getBean("helloBean");  
  6. System.out.println(hello.getHelloWord());  
  7. }  

二、使用constructor方式完成注入

 

 
  1. public class HelloBean {  
  2. private String name;  
  3. private String helloWord;  
  4. // 建议有要无参数建构方法  
  5. public HelloBean() {  
  6. }  
  7. public HelloBean(String name, String helloWord) {  
  8. this.name = name;  
  9. this.helloWord = helloWord;  
  10. }  
  11. //...省略getter、setter方法  

 

 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"  
  3. "http://www.springframework.org/dtd/spring-beans.dtd">  
  4. <beans>  
  5. <bean id="helloBean" 
  6. class="onlyfun.caterpillar.HelloBean">  
  7. <constructor-arg index="0">  
  8. <value>Justin</value>  
  9. </constructor-arg>  
  10. <constructor-arg index="1">  
  11. <value>Hello</value>  
  12. </constructor-arg>  
  13. </bean>  
  14. </beans> 

 

 
  1. public class SpringDemo {  
  2. public static void main(String[] args) {  
  3. ApplicationContext context =  
  4. new FileSystemXmlApplicationContext("beans-config.xml");  
  5. HelloBean hello = (HelloBean) context.getBean("helloBean");  
  6. System.out.print("Name: ");  
  7. System.out.println(hello.getName());  
  8. System.out.print("Word: ");  
  9. System.out.println(hello.getHelloWord());  
  10. }  

三、属性参考

 

 
  1. public class HelloBean {  
  2. private String helloWord;  
  3. private Date date;  
  4. //...省略getter、setter方法  

 

 
  1. <beans>  
  2. <bean id="dateBean" class="java.util.Date"/>  
  3. <bean id="helloBean" class="onlyfun.caterpillar.HelloBean">  
  4. <property name="helloWord">  
  5. <value>Hello!</value>  
  6. </property>  
  7. <property name="date">  
  8. <ref bean="dateBean"/>  
  9. </property>  
  10. </bean>  
  11. </beans> 

 

 
  1. public class SpringDemo {  
  2. public static void main(String[] args) {  
  3. ApplicationContext context =  
  4. new FileSystemXmlApplicationContext("beans-config.xml");  
  5. HelloBean hello = (HelloBean) context.getBean("helloBean");  
  6. System.out.print(hello.getHelloWord());  
  7. System.out.print(" It's ");  
  8. System.out.print(hello.getDate());  
  9. System.out.println(".");  
  10. }  

四、“byType”自动绑定

将“三”中的配置文件改为下面,即可完成bean属性的按类型自动绑定。

 

 
  1. <beans>  
  2. <bean id="dateBean" class="java.util.Date"/>  
  3. <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byType">  
  4. <property name="helloWord">  
  5. <value>Hello!</value>  
  6. </property>  
  7. </bean>  
  8. </beans> 

五、“byName”自动绑定

将“三”中的配置文件改为下面,即可完成bean属性的按名称自动绑定。

 

 
  1. <beans>  
  2. <bean id="dateBean" class="java.util.Date"/>  
  3. <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byName">  
  4. <property name="helloWord">  
  5. <value>Hello!</value>  
  6. </property>  
  7. </bean>  
  8. </beans> 

六、“constructor”自动绑定

将“三”中的配置文件改为下面,即可完成bean属性的按构造方法自动绑定。在建立依赖关系时,Srping容器会试图比对容器中的Bean实例类型,及相关的构造方法上的参数类型,看看在类型上是否符合,如果有的话,则选用该构造方法来建立Bean实例。如果无法绑定,则抛出org.springframework.beans.factory.UnsatisfiedDependencyException异常。

 

 
  1. <beans>  
  2. <bean id="dateBean" class="java.util.Date"/>  
  3. <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="constructor">  
  4. <property name="helloWord">  
  5. <value>Hello!</value>  
  6. </property>  
  7. </bean>  
  8. </beans> 

六、“autodetect”自动绑定

将“三”中的配置文件改为下面,即可完成bean属性的自动绑定,这个自动绑定是Spring会尝试用入constructor来处理依赖关系的建立,如果不行,则再尝试用byType类建立依赖关系。

 

 
  1. <beans>  
  2. <bean id="dateBean" class="java.util.Date"/>  
  3. <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="autodetect">  
  4. <property name="helloWord">  
  5. <value>Hello!</value>  
  6. </property>  
  7. </bean>  
  8. </beans> 

七、依赖检查方式

在自动绑定中,由于没办法从定义文件中,清楚地看到是否每个属性都完成设定,为了确定某些依赖关系确实建立,您可以假如依赖检查,在标签使用时设定"dependency-check",可以有四种依赖检查方式:simple、objects、all、none。

simple:只检查简单的类型(像原生数据类型或字符串对象)属性是否完成依赖关系,。

objects:检查对象类型的属性是否完成依赖关系。

all:则检查全部的属性是否完成依赖关系。

none:设定是默认值,表示不检查依赖性。

 

 
  1. <beans>  
  2. <bean id="dateBean" class="java.util.Date"/>  
  3. <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="autodetect" dependeny-check="all">  
  4. <property name="helloWord">  
  5. <value>Hello!</value>  
  6. </property>  
  7. </bean>  
  8. </beans> 

八、集合对象注入

对于像数组、List、Set、Map等集合对象,在注入前必须填充一些对象至集合中,然后再将集合对象注入至所需的Bean时,也可以交由Spring的IoC容器来自动维护或生成集合对象,并完成依赖注入。

 

 
  1. public class SomeBean {  
  2. private String[] someStrArray;  
  3. private Some[] someObjArray;  
  4. private List someList;  
  5. private Map someMap;  
  6. public String[] getSomeStrArray() {  
  7. return someStrArray;  
  8. }  
  9. public void setSomeStrArray(String[] someStrArray) {  
  10. this.someStrArray = someStrArray;  
  11. }  
  12. public Some[] getSomeObjArray() {  
  13. return someObjArray;  
  14. }  
  15. public void setSomeObjArray(Some[] someObjArray) {  
  16. this.someObjArray = someObjArray;  
  17. }  
  18. public List getSomeList() {  
  19. return someList;  
  20. }  
  21. public void setSomeList(List someList) {  
  22. this.someList = someList;  
  23. }  
  24. public Map getSomeMap() {  
  25. return someMap;  
  26. }  
  27. public void setSomeMap(Map someMap) {  
  28. this.someMap = someMap;  
  29. }  
  30. public class Some {  
  31. private String name;  
  32. public String getName() {  
  33. return name;  
  34. }  
  35. public void setName(String name) {  
  36. this.name = name;  
  37. }  
  38. public String toString() {  
  39. return name;  
  40. }  

 

 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"  
  3. "http://www.springframework.org/dtd/spring-beans.dtd">  
  4. <beans>  
  5. <bean id="some1" class="onlyfun.caterpillar.Some">  
  6. <property name="name">  
  7. <value>Justin</value>  
  8. </property>  
  9. </bean>  
  10. <bean id="some2" class="onlyfun.caterpillar.Some">  
  11. <property name="name">  
  12. <value>momor</value>  
  13. </property>  
  14. </bean>  
  15. <bean id="someBean" class="onlyfun.caterpillar.SomeBean">  
  16. <property name="someStrArray">  
  17. <list>  
  18. <value>Hello</value>  
  19. <value>Welcome</value>  
  20. </list>  
  21. </property>  
  22. <property name="someObjArray">  
  23. <list>  
  24. <ref bean="some1"/>  
  25. <ref bean="some2"/>  
  26. </list>  
  27. </property>  
  28. <property name="someList">  
  29. <list>  
  30. <value>ListTest</value>  
  31. <ref bean="some1"/>  
  32. <ref bean="some2"/>  
  33. </list>  
  34. </property>  
  35. <property name="someMap">  
  36. <map>  
  37. <entry key="MapTest">  
  38. <value>Hello!Justin!</value>  
  39. </entry>  
  40. <entry key="someKey1">  
  41. <ref bean="some1"/>  
  42. </entry>  
  43. </map>  
  44. </property>  
  45. </bean>  
  46. </beans> 

 

 
  1. public class SpringDemo {  
  2. public static void main(String[] args) {  
  3. ApplicationContext context =  
  4. new FileSystemXmlApplicationContext(  
  5. "beans-config.xml");  
  6. SomeBean someBean =  
  7. (SomeBean) context.getBean("someBean");  
  8. // 取得数组型态依赖注入对象  
  9. String[] strs =  
  10. (String[]) someBean.getSomeStrArray();  
  11. Some[] somes =  
  12. (Some[]) someBean.getSomeObjArray();  
  13. for(int i = 0; i < strs.length; i++) {  
  14. System.out.println(strs[i] + "," 
  15. + somes[i].getName());  
  16. }  
  17. // 取得List型态依赖注入对象  
  18. System.out.println();  
  19. List someList = (List) someBean.getSomeList();  
  20. for(int i = 0; i < someList.size(); i++) {  
  21. System.out.println(someList.get(i));  
  22. }  
  23. // 取得Map型态依赖注入对象  
  24. System.out.println();  
  25. Map someMap = (Map) someBean.getSomeMap();  
  26. System.out.println(someMap.get("MapTest"));  
  27. System.out.println(someMap.get("someKey1"));  
  28. }  

希望本文所述对大家Java程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表