1. package com.ysq.vo;
  2. public class User {
  3. private int uid;
  4. private String uname;
  5. private String pwd;
  6. private Date birth;
  7. private String[] likes; //声明一个数组
  8. private List<String> list; //声明一个集合
  9. private Map<String, String> map; //声明一个map
  10. public User() {
  11. super();
  12. }
  13. public User(String uname, String pwd) {
  14. super();
  15. this.uname = uname;
  16. this.pwd = pwd;
  17. }
  18. public User(int uid, String uname, String pwd) {
  19. super();
  20. this.uid = uid;
  21. this.uname = uname;
  22. this.pwd = pwd;
  23. }
  24.  
  25. public User(String uname, String pwd, Date birth) {
  26. super();
  27. this.uname = uname;
  28. this.pwd = pwd;
  29. this.birth = birth;
  30. }
  31. 以省略了对应的getset方法
  32. }

1. Constructor injection 构造函数注入

1.1 在Bean的配置文件中,使用<constructor-arg>来表示将使用Constructor injection,由于使用Constructor injection时并不如Setter injection时拥有setXXX()这样易懂的名称,所以必须指定参数的位置索引,index属性就是用于指定对象将注入至建构函式中的哪一个参数,参数的顺序指定中,第一个参数的索引值是0,第二个是1,依此类推。 示例配置:

  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="cons_3" class="com.ysq.vo.User">
     <constructor-arg index="0" value="111"/>
     <constructor-arg index="1" value="李四"/>
     <constructor-arg index="2" value="1212"/>
    </bean>
    </beans>

1.2 如果constructor上只有一个参数,则不必指定index属性。 示例配置:

  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="cons_4" class="com.ysq.vo.User">
     <constructor-arg value="李四"/>
    </bean>
  6. </beans>

1.3 若有两个以上的参数,而参数类型各不相同的话,可以使用type来指定constructor上的参数类型。 示例代码

配置:

  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="cons_4" class="com.ysq.vo.User">
     <constructor-arg type="java.lang.Integer">
       <value>1111</value>
     </constructor-arg>
     <constructor-arg type="java.lang.String">
     <value>张三</value>
     </constructor-arg>
     <constructor-arg type="java.lang.String">
     <value>112222</value>
     </constructor-arg>
    </bean>
    </beans>

2. property injection (通过属性注入)

2.1 直接指定值或是使用<ref>直接指定参考至其它的Bean 示例配置:

  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.  
  5. <beans>
  6. <bean id="dateBean" class="java.util.Date"/>
  7.  
  8. <bean id="helloBean"  class="com.ysq.vo.User">
    <property name="uname">
  9. <value>张三!</value>
  10. </property>
  11. <property name="birth">
  12. <ref bean="dateBean"/><!--引入bean文件-->
  13. </property>
  14. </bean>
  15. </beans>

2.2 也可以用内部Bean的方式来注入依赖关系 示例配置:

  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.  
  5. <beans>
  6. <bean id="helloBean" class="org.sixsun.spring.HelloBean">
  7. <property name="uname">
  8. <value>Hello!</value>
  9. </property>
  10. <property name="date">
  11. <bean id="dateBean" class="java.util.Date"/>
  12. </property>
  13. </bean>
  14. </beans>

3. 自动绑定

直接指定值或是使用<ref>直接指定参考至其它的Bean,Spring也支持隐式的自动绑定,您可以通过类型(byType)或名称(byName)将Bean绑定至其它Bean上对应的属性: 3.1 byType 在配置文件中,并没有指定helloBean的Date属性,而是透过自动绑定,由于autowire指定了byType,所以会根据helloBean 的 Date属性所接受的类型,判断是否有类似的类型对象,并将之绑定至helloBean的Date属性上,如果byType无法完成绑定,则抛出 org.springrframework.beans.factory.UnsatisfiedDependencyExcpetion异常。 示例配置:

  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.  
  5. <beans>
  6. <bean id="dateBean" class="java.util.Date"/>
  7. <bean id="helloBean"
  8.  class="com.ysq.vo.User"
    autowire="byType">
  9. <property name="uname">
  10. <value>Hello!</value>
  11. </property>
  12. </bean>
  13. </beans>

3.2 byName 也可以指定byName来绑定,则Spring会根据bean的别名与属性名称是否符合来进行自动绑定,举个例子来说,如果是byName而Date属性要完成依赖注入的话,则必须修改一下第一个Bean的id值为date: 示例配置:

  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.  
  5. <beans>
  6. <bean id="date" class="java.util.Date"/>
  7. <bean id="helloBean"
  8.  class="com.ysq.vo.User"
    autowire="byName">
  9. <property name="uname">
  10. <value>Hello!</value>
  11. </property>
  12. </bean>
  13. </beans>

3.3 constructor也可以尝试进行自动绑定 由于autowire设定为constructor,在建立绑定关系时,Spring容器会试图比对容器中的Bean及相关的建构方法,在类型上是否有符合,如果有的话,则选用该建构方法来建立Bean实例,例如上例中,HelloBean的带参数建构方法与date这个Bean的类型相符,于是选用该构造方法来建构实例,并将date这个Bean注入给它,如果无法完成绑定,则抛出 org.springframework.beans.factory.UnsatisfiedDependencyException异常。 示例配置:

  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.  
  5. <beans>
  6. <bean id="date" class="java.util.Date"/>
  7. <bean id="helloBean"
  8.  class="com.ysq.vo.User"
    autowire="constructor">
  9. <property name="uname">
  10. <value>Hello!</value>
  11. </property>
  12. </bean>
  13. </beans>

3.4 autodetect 使用autodetect时,会尝试使用constructor,然后使用byType,哪一个先符合就先用。 示例配置:

  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.  
  5. <beans>
  6. <bean id="date" class="java.util.Date"/>
  7. <bean id="helloBean"
  8.  class="com.ysq.vo.User"
    autowire="autodetect">
  9. <property name="uname">
  10. <value>Hello!</value>
  11. </property>
  12. </bean>
  13. </beans>

3.5 其它: 隐式的自动绑定没办法从配置文件中清楚的看到是否每个属性都完成设定,我们可以加入相依检查,在<bean>上加入dependency- check,有四种相依检查方式:simple、objects、all、none。 simple只检查简单的属性,像是原生(primitive)数据型态或字符串对象; objects检查对象属性; all则检查全部的属性; none是预设,表示不检查相依性; 示例配置:

  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.  
  5. <beans>
  6. <bean id="date" class="java.util.Date"/>
  7. <bean id="helloBean"
  8.  class="com.ysq.vo.User"
    autowire="autodetect"
  9. dependency-check="all">
  10. <property name="uname">
  11. <value>Hello!</value>
  12. </property>
  13. </bean>
  14. </beans>

4. 集合对象注入:

对于像数组、java.util.List、java.util.Set、java.util.Map等集合对象,在注入前必须填充入一些对象至集合中,然后再将集合对象注入至所需的Bean中,例如若有个Bean如下:

示例代码:

  1. public class SomeBean {
  2. private String[] someStrArray;
  3. private SomeObj[] someObjArray;
  4. private List someList;
  5. private Map someMap;
  6. ....
  7.  
  8. }

示例配置:

  1. <beans>
  2. <bean id="someBean" class="onlyfun.caterpillar.SomeBean">
  3. <property name="someArray">
  4. <list>
  5. <value>Hello!Justin!</value>
  6. <value>Hello!Momor!</value>
  7. <value>Hello!Bush!</value>
  8. </list>
  9. </property>
  10. <property name="someObjArray">
  11. <list>
  12. <ref bean="someObj1"/>
  13. <ref bean="someObj2"/>
  14. </list>
  15. </property>
  16. <property name="someList">
  17. <list>
  18. <value>Hello!Justin!</value>
  19. <ref bean="someObj1"/>
  20. <ref bean="someObj2"/>
  21. </list>
  22. </property>
  23. <property name="someMap">
  24. <map>
  25. <entry key="somekey1">
  26. <ref bean="someObj1"/>
  27. </entry>
  28. <entry key="somekey2">
  29. <value>Hello!Justin!</value>
  30. </entry>
  31. </map>
  32. </property>
  33. </bean>
  34. </beans>

上面的Bean定义文件是个综合示范,数组与List对象都是用<list>卷标来设定,而Map对象使用<map>卷标设定,并需要一个key值设定。 Set的使用<set>标签,例如:

  1. <set>
  2. <value>a set element</value>
  3. <ref bean="otherBean"/>
  4. <ref bean="anotherBean"/>
  5. </set>

您也可以注入java.util.Properties,Bean定义档的写法示范如下:

  1. <bean id=....>
  2. ....
  3. <property name="someProperties">
  4. <props>
  5. <prop key="someProkey1">
  6. someProValue1
  7. </prop>
  8. <prop key="someProkey2">
  9. someProValue2
  10. </prop>
  11. </props>
  12. </property>
  13. </bean>

Spring 中设置依赖注入的更多相关文章

  1. JavaEE开发之Spring中的依赖注入与AOP

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  2. JavaEE开发之Spring中的依赖注入与AOP编程

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  3. Spring学习(三)——Spring中的依赖注入的方式

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  4. Spring学习(一)——Spring中的依赖注入简介【转】

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  5. Spring学习(一)——Spring中的依赖注入简介

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  6. spring中的依赖注入

    Ioc的作用: 降低程序间的耦合(依赖关系) 依赖关系的管理: 以后都交给Spring来维护 在当前类需要用到其他类的对象,由Spring为我们提供, 我们只需要在配置文件中说明 依赖关系的维护,就称 ...

  7. spring中的依赖注入(DI)笔记

    使用xml bean依赖注入有set注入和构造器注入 set注入用的比较多 <bean id="a" class="com.A"> <prop ...

  8. 类比Spring框架来实现OC中的依赖注入

    如果你之前使用过JavaEE开发中的Spring框架的话,那么你一定对依赖注入并不陌生.依赖注入(DI: Dependency Injection)是控制反转(IoC: Inversion of Co ...

  9. 谈谈自己了解的spring.NET的依赖注入

         spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection),以达到解耦的目的,实现模块的组件化.程序 ...

随机推荐

  1. Making the Elephant Dance: Strategic Enterprise Analysis

    http://www.modernanalyst.com/Resources/Articles/tabid/115/ID/2934/categoryId/23/Making-the-Elephant- ...

  2. ios9中 UIStackView的使用

    ios9中 UIStackView的使用 by 伍雪颖 UIStackView能够垂直或水平排布多个subview, 自己主动为每一个subview创建和加入Auto Layout constrain ...

  3. [React] React Fundamentals: JSX Deep Dive

    "JSX transforms from an XML-like syntax into native JavaScript. XML elements and attributes are ...

  4. KDB调试内核

    http://www.ibm.com/developerworks/cn/linux/l-kdbug/

  5. windows 下rabbitmq 安装---转载

    原文地址:http://blog.sina.com.cn/s/blog_7cc0c8cc0101mb4a.html 1.下载并安装erlang,http://www.erlang.org/downlo ...

  6. SDWebImage 原理及使用

    这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下载.同一个URL下载次数控制和优化等特征. SDWebImage 加载图片的流程 入口 setImageWi ...

  7. javascript中的call()和apply应用

    在javascript开发过程中,如果有看过几个javascirpt代码库,就会发现经常使用到call()和apply()函数,call()和aplly()结合javascript允许传递函数名,这种 ...

  8. SQL排序 空值的后面

    按sort排序,sort为空的在后面 end),sort

  9. 转-C# 操作 Excel 常见问题收集和整理

    经常会有项目需要把表格导出为 Excel 文件,或者是导入一份 Excel 来操作,那么如何在 C# 中操作 Excel 文件成了一个最基本的问题. 做开发这几年来,陆陆续续也接触过这样的需求,但因为 ...

  10. Oracle学习【索引及触发器】

    索引B_Tree结构 请参照 响应图例 索引是一种允许直接访问数据表中某一数据行的树形结构,为了提高查询效率而引入,是独立于表的对象,可以存放在与表不同的表空间中.索引记录中存有索引关键字和指向表中数 ...