【第三章】 DI 之 3.1.3 DI的配置使用 ——跟我学spring3

 四、其他引用方式:除了最基本配置方式以外,Spring还提供了另外两种更高级的配置方式,<ref local=””/>和<ref parent=””/>

       (1)<ref local=””/>配置方式:用于引用通过<bean id=”beanName”>方式中通过id属性指定的Bean,它能利用XML解析器的验证功能在读取配置文件时来验证引用的Bean是否存在。因此如果在当前配置文件中有相互引用的Bean可以采用<ref local>方式从而如果配置错误能在开发调试时就发现错误。

如果引用一个在当前配置文件中不存在的Bean将抛出如下异常:

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line21 inXML document from class path resource [chapter3/beanInject2.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-id.1: There is no ID/IDREF binding for IDREF 'helloApi'.

<ref local>具体配置方式如下:

 

       (2)<ref parent=””/>配置方式:用于引用父容器中的Bean,不会引用当前容器中的Bean,当然父容器中的Bean和当前容器的Bean是可以重名的,获取顺序是直接到父容器找。具体配置方式如下:

接下来让我们用个例子演示一下<ref local>和<ref parent>的配置过程:

首先还是准备测试类,在此我们就使用以前写好的HelloApiDecorator和HelloImpl4类;其次进行Bean定义,其中当前容器bean1引用本地的”helloApi”,而”bean2”将引用父容器的”helloApi”,配置如下:

 

java代码:

查看复制到剪贴板打印

  1. <!-- sources/chapter3/parentBeanInject.xml表示父容器配置-->  

  2. <!--注意此处可能子容器也定义一个该Bean-->  

  3. <bean id="helloApi" class="cn.javass.spring.chapter3.HelloImpl4">  

  4. <property name="index" value="1"/>  

  5. <property name="message" value="Hello Parent!"/>  

  6. </bean>  

 

 

java代码:

查看复制到剪贴板打印

  1. <!-- sources/chapter3/localBeanInject.xml表示当前容器配置-->  

  2. <!-- 注意父容器中也定义了id 为 helloApi的Bean -->  

  3. <bean id="helloApi" class="cn.javass.spring.chapter3.HelloImpl4">  

  4. <property name="index" value="1"/>  

  5.     <property name="message" value="Hello Local!"/>  

  6. </bean>  

  7. <!-- 通过local注入 -->  

  8. <bean id="bean1" class="cn.javass.spring.chapter3.bean.HelloApiDecorator">  

  9. <constructor-arg index="0"><ref local="helloApi"/></constructor-arg>  

  10. </bean>  

  11. <!-- 通过parent注入 -->  

  12. <bean id="bean2" class="cn.javass.spring.chapter3.bean.HelloApiDecorator">  

  13. <property name="helloApi"><ref parent="helloApi"/></property>  

  14. </bean>  

 

(3)写测试类测试一下吧,具体代码片段如下: 

 

java代码:

查看复制到剪贴板打印

  1. @Test   

  2. public void testLocalAndparentBeanInject() {  

  3. //初始化父容器  

  4. ApplicationContext parentBeanContext =  

  5. new ClassPathXmlApplicationContext("chapter3/parentBeanInject.xml");  

  6. //初始化当前容器  

  7. ApplicationContext beanContext = new ClassPathXmlApplicationContext(  

  8. new String[] {"chapter3/localBeanInject.xml"}, parentBeanContext);  

  9.     HelloApi bean1 = beanContext.getBean("bean1", HelloApi.class);  

  10.     bean1.sayHello();//该Bean引用local bean  

  11. HelloApi bean2 = beanContext.getBean("bean2", HelloApi.class);  

  12. bean2.sayHello();//该Bean引用parent bean  

  13. }  

 

 “bean1”将输出“Hello Local!”表示引用当前容器的Bean,”bean2”将输出“Hello Paren!”,表示引用父容器的Bean,如配置有问题请参考cn.javass.spring.chapter3.DependencyInjectTest中的testLocalAndparentBeanInject测试方法。

 

3.1.8  内部Bean定义

内部Bean就是在<property>或<constructor-arg>内通过<bean>标签定义的Bean,该Bean不管是否指定id或name,该Bean都会有唯一的匿名标识符,而且不能指定别名,该内部Bean对其他外部Bean不可见,具体配置如下:

 

(1)让我们写个例子测试一下吧,具体配置文件如下:

 

java代码:

查看复制到剪贴板打印

  1. <bean id="bean" class="cn.javass.spring.chapter3.bean.HelloApiDecorator">  

  2. <property name="helloApi">  

  3. <bean id="helloApi" class="cn.javass.spring.chapter2.helloworld.HelloImpl"/>  

  4. </property>  

  5. </bean>  

 

 

 (2)测试代码(cn.javass.spring.chapter3.DependencyInjectTest.testInnerBeanInject):

 

java代码:

查看复制到剪贴板打印

  1. @Test   

  2. public void testInnerBeanInject() {  

  3. ApplicationContext context =  

  4. new ClassPathXmlApplicationContext("chapter3/innerBeanInject.xml");  

  5. HelloApi bean = context.getBean("bean", HelloApi.class);  

  6. bean.sayHello();  

  7. }  

 

3.1.9  处理null值

        Spring通过<value>标签或value属性注入常量值,所有注入的数据都是字符串,那如何注入null值呢?通过“null”值吗?当然不是因为如果注入“null”则认为是字符串。Spring通过<null/>标签注入null值。即可以采用如下配置方式:

 

3.1.10 对象图导航注入支持

       所谓对象图导航是指类似a.b.c这种点缀访问形式的访问或修改值。Spring支持对象图导航方式依赖注入。对象图导航依赖注入有一个限制就是比如a.b.c对象导航图注入中a和b必须为非null值才能注入c,否则将抛出空指针异常。

       Spring不仅支持对象的导航,还支持数组、列表、字典、Properties数据类型的导航,对Set数据类型无法支持,因为无法导航。

数组和列表数据类型可以用array[0]、list[1]导航,注意”[]”里的必须是数字,因为是按照索引进行导航,对于数组类型注意不要数组越界错误。

字典Map数据类型可以使用map[1]、map[str]进行导航,其中“[]”里的是基本类型,无法放置引用类型。

       让我们来练习一下吧。首先准备测试类,在此我们需要三个测试类,以便实现对象图导航功能演示:

        NavigationC类用于打印测试代码,从而观察配置是否正确;具体类如下所示:

 

java代码:

查看复制到剪贴板打印

  1. package cn.javass.spring.chapter3.bean;  

  2. public class NavigationC {  

  3.     public void sayNavigation() {  

  4.         System.out.println("===navigation c");  

  5.     }  

  6. }  

 

    NavigationB类,包含对象和列表、Properties、数组字典数据类型导航,而且这些复合数据类型保存的条目都是对象,正好练习一下如何往复合数据类型中注入对象依赖。具体类如下所示:

 

 

java代码:

查看复制到剪贴板打印

  1. package cn.javass.spring.chapter3.bean;  

  2. import java.util.List;  

  3. import java.util.Map;  

  4. import java.util.Properties;  

  5. public class NavigationB {  

  6.     private NavigationC navigationC;  

  7.     private List<NavigationC> list;  

  8.     private Properties properties;  

  9.     private NavigationC[] array = new NavigationC[1];  

  10.     private Map<String, NavigationC> map;  

  11.    //由于setter和getter方法占用太多空间,故省略,大家自己实现吧  

  12. }  

 

        NavigationA类是我们的前端类,通过对它的导航进行注入值,具体代码如下:

 

java代码:

查看复制到剪贴板打印

  1. package cn.javass.spring.chapter3.bean;  

  2. public class NavigationA {  

  3.     private NavigationB navigationB;  

  4.     public void setNavigationB(NavigationB navigationB) {  

  5.         this.navigationB = navigationB;  

  6.     }  

  7.     public NavigationB getNavigationB() {  

  8.         return navigationB;  

  9.     }  

  10. }  

 

        接下来该进行Bean定义配置(resources/chapter3/navigationBeanInject.xml)了,首先让我们配置一下需要被导航的数据,NavigationC和NavigationB类,其中配置NavigationB时注意要确保比如array字段不为空值,这就需要或者在代码中赋值如“NavigationC[] array = new NavigationC[1];”,或者通过配置文件注入如“<list></list>”注入一个不包含条目的列表。具体配置如下:

 

java代码:

查看复制到剪贴板打印

  1. <bean id="c" class="cn.javass.spring.chapter3.bean.NavigationC"/>  

  2. <bean id="b" class="cn.javass.spring.chapter3.bean.NavigationB">  

  3. <property name="list"><list></list></property>  

  4.     <property name="map"><map></map></property>  

  5.     <property name="properties"><props></props></property>  

  6. </bean>  

 

         配置完需要被导航的Bean定义了,该来配置NavigationA导航Bean了,在此需要注意,由于“navigationB”属性为空值,在此需要首先注入“navigationB”值;还有对于数组导航不能越界否则报错;具体配置如下:

 

java代码:

查看复制到剪贴板打印

  1. <bean id="a" class="cn.javass.spring.chapter3.bean.NavigationA">  

  2. <!-- 首先注入navigatiionB 确保它非空 -->  

  3. <property name="navigationB" ref="b"/>  

  4. <!-- 对象图导航注入 -->  

  5. <property name="navigationB.navigationC" ref="c"/>  

  6. <!-- 注入列表数据类型数据 -->  

  7. <property name="navigationB.list[0]" ref="c"/>  

  8. <!-- 注入map类型数据 -->  

  9. <property name="navigationB.map[key]" ref="c"/>  

  10. <!-- 注入properties类型数据 -->  

  11. <property name="navigationB.properties[0]" ref="c"/>  

  12. <!-- 注入properties类型数据 -->  

  13. <property name="navigationB.properties[1]" ref="c"/>  

  14. <!-- 注入数组类型数据 ,注意不要越界-->  

  15. <property name="navigationB.array[0]" ref="c"/>  

  16. </bean>  

  17.    

        配置完毕,具体测试代码在cn.javass.spring.chapter3. DependencyInjectTest,让我们看下测试代码吧:

 

java代码:

查看复制到剪贴板打印

  1. //对象图导航  

  2. @Test   

  3. public void testNavigationBeanInject() {  

  4. ApplicationContext context =  

  5. new ClassPathXmlApplicationContext("chapter3/navigationBeanInject.xml");         

  6. NavigationA navigationA = context.getBean("a", NavigationA.class);  

  7. navigationA.getNavigationB().getNavigationC().sayNavigation();  

  8. navigationA.getNavigationB().getList().get(0).sayNavigation();  

  9. navigationA.getNavigationB().getMap().get("key").sayNavigation();  

  10. navigationA.getNavigationB().getArray()[0].sayNavigation();  

  11. ((NavigationC)navigationA.getNavigationB().getProperties().get("1"))  

  12. .sayNavigation();         

  13. }  

  14.    

 

       测试完毕,应该输出5个“===navigation c”,是不是很简单,注意这种方式是不推荐使用的,了解一下就够了,最好使用3.1.5一节使用的配置方式。

3.1.11配置简写

让我们来总结一下依赖注入配置及简写形式,其实我们已经在以上部分穿插着进行简化配置了:

 

一、构造器注入:

1)常量值

简写:<constructor-arg index="0" value="常量"/>

全写:<constructor-arg index="0"><value>常量</value></constructor-arg>

2)引用

简写:<constructor-arg index="0" ref="引用"/>

全写:<constructor-arg index="0"><ref bean="引用"/></constructor-arg>

 

二、setter注入:      

       1)常量值

        简写:<property name="message" value="常量"/>

        全写:<property name="message"><value>常量</value></ property>

       2)引用

        简写:<property name="message" ref="引用"/>

        全写:<property name="message"><ref bean="引用"/></ property>

       3)数组:<array>没有简写形式

       4)列表:<list>没有简写形式

       5)集合:<set>没有简写形式

       6)字典

          简写:<map>

             <entry key="键常量" value="值常量"/>

             <entry key-ref="键引用" value-ref="值引用"/>

            </map>

         全写:<map>

             <entry><key><value>键常量</value></key><value>值常量</value></entry>

             <entry><key><ref bean="键引用"/></key><ref bean="值引用"/></entry>

           </map>

       7)Properties:没有简写形式

 

三、使用p命名空间简化setter注入:

       使用p命名空间来简化setter注入,具体使用如下:

 

 

java代码:

查看复制到剪贴板打印

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <beans  xmlns="http://www.springframework.org/schema/beans"  

  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

  4.         xmlns:p="http://www.springframework.org/schema/p"  

  5.         xsi:schemaLocation="  

  6.            http://www.springframework.org/schema/beans  

  7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

  8. <bean id="bean1" class="java.lang.String">  

  9.         <constructor-arg index="0" value="test"/>  

  10.     </bean>  

  11. <bean id="idrefBean1" class="cn.javass.spring.chapter3.bean.IdRefTestBean"  

  12. p:id="value"/>  

  13. <bean id="idrefBean2" class="cn.javass.spring.chapter3.bean.IdRefTestBean"  

  14. p:id-ref="bean1"/>  

  15. </beans>  

  • xmlns:p="http://www.springframework.org/schema/p" :首先指定p命名空间;

  • <bean id="……" class="……" p:id="value"/> 常量setter注入方式,其等价于<property name="id" value="value"/>;

    • <bean id="……" class="……" p:id-ref="bean1"/> 引用setter注入方式,其等价于<property name="id" ref="bean1"/>。 

    •  

原创内容,转载请注明【http://sishuok.com/forum/posts/list/2447.html


转载于:https://my.oschina.net/u/435621/blog/401791

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值