DI (Dependency Injection)

1、依赖注入,组件之间的依赖关系由容器在运行期间决定。Ioc容器注入应用程序某个对象,它所需要的外部资源(包括对象,资源,常量数据)。

birthdate需要用到date类型的一个对象,引用了d的对象就是一种DI(依赖注入),s.setBirthdate( d )依赖一个date类型的对象,注入就是将date类型的对象set进去。

  1. <bean id="d" class="java.util.Date" />
  2. <bean id="s" class="io.spring.ioc.base.Student" >
  3. <property name="id" value="1001" />
  4. <property name="name" value="张三丰" />
  5. <property name="gender" value="男" />
  6. <property name="birthdate" ref="d" />
  7. </bean>

相当于s.setBirthdate( d ),也可以指定内部的bean

  1. <bean id="s" class="io.spring.ioc.base.Student">
  2. <property name="id" value="1001" />
  3. <property name="name" value="张三丰" />
  4. <property name="gender" value="男" />
  5. <property name="birthdate" >
  6. <bean class="java.util.Date" />
  7. </property>
  8. </bean>

相当于s.setBirthdate( new Date )。

Spring Ioc容器

1、具有依赖注入功能的容器。负责实例化,定位,配置应用程序中的对象及建立这些对象间的依赖。在Spring中BeanFactory是Ioc容器的实际代表者。BeanFactory接口提供了IoC容器的基本功能。Spring Ioc容器通过读取配置文件中的配置元数据,通过元数据对应用中各个对象进行实例化及装配。

Spring IoC容器注入依赖资源的方式

1、Spring IoC容器注入依赖资源的方式有两种setter注入和构造方法注入。

2、setter注入, ( setter-base ),提供setter方法进行注入,依赖于无参构造和setter方法

  • Spring Framework 3.0 之前 ,使用property标记来来指定属性的注入值

    1. <bean id="customer" class="ecut.ioc.di.Customer" > c.setId(1002)
    2. <property name="id" value="1002" />
    3. <property name="name" value="张翠山" />
    4. <property name="gender" value="男" />
    5. <property name="birthdate" ref="date" />
    6. </bean>

    通过bean标记来声明这个bean,使用bean标记的id或name属性来指定bean的名称 , bean的class属相来指定bean的类型(具体的不是抽象的,不然需要修改abstract属性为true),name属性指定setter方法对应的名称,比如setBirthdate就写birthdate,ref属性用来指定需要引用的那个已经存在的bean的名称,普通类型用value,需要引用的类型使用ref。

  • Spring Framework 3.0 开始支持 p 命名空间为属性注入值 ,也依赖于无参构造和setter方法
    1. <!-- setter方法注入, 使用P命名空间(property namespace,需要提供p命名空间)-->
    2. <bean id="customer"
    3. class="ecut.ioc.di.Customer"
    4. p:id="1002"
    5. p:name="张翠山"
    6. p:gender="男"
    7. p:birthdate-ref="date" />

    普通类型p:name="value",需要引用的类型使用p:name-ref="引用bean的id"

  • 测试案例

    Customer类

    1. package ecut.ioc.di;
    2.  
    3. import java.util.Date;
    4.  
    5. public class Customer {
    6.  
    7. private Integer id;
    8. private String name;
    9. private char gender;
    10. private Date birthdate;
    11.  
    12. public Customer() {
    13. super();
    14. System.out.println( "调用 Customer 无参构造创建对象" );
    15. }
    16.  
    17. public Customer(Integer id, String name, char gender) {
    18. super();
    19. this.id = id;
    20. this.name = name;
    21. this.gender = gender;
    22. System.out.println( "调用 Customer( Integer , String , char ) 创建对象" );
    23. }
    24.  
    25. public Customer(Integer id, String name, char gender, Date birthdate) {
    26. super();
    27. this.id = id;
    28. this.name = name;
    29. this.gender = gender;
    30. this.birthdate = birthdate;
    31. System.out.println( "调用 Customer( Integer , String , char , Date ) 创建对象" );
    32. }
    33.  
    34. public Integer getId() {
    35. return id;
    36. }
    37.  
    38. public void setId(Integer id) {
    39. this.id = id;
    40. System.out.println( "为id属性赋值: " + id );
    41. }
    42.  
    43. public String getName() {
    44. return name;
    45. }
    46.  
    47. public void setName(String name) {
    48. this.name = name;
    49. System.out.println( "为name属性赋值: " + name );
    50. }
    51.  
    52. public char getGender() {
    53. return gender;
    54. }
    55.  
    56. public void setGender(char gender) {
    57. this.gender = gender;
    58. System.out.println( "为gender属性赋值: " + gender );
    59. }
    60.  
    61. public Date getBirthdate() {
    62. return birthdate;
    63. }
    64.  
    65. public void setBirthdate(Date birthdate) {
    66. this.birthdate = birthdate;
    67. System.out.println( "为birthdate属性赋值: " + birthdate );
    68. }
    69.  
    70. }

    setter-injection.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2.  
    3. <beans xmlns="http://www.springframework.org/schema/beans"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:p="http://www.springframework.org/schema/p"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
    7.  
    8. <bean id="date" class="java.util.Date" />
    9.  
    10. <!-- setter方法注入, 使用P命名空间(property namespace,需要提供p命名空间)-->
    11. <bean id="customer"
    12. class="ecut.ioc.di.Customer"
    13. p:id="1002"
    14. p:name="张翠山"
    15. p:gender="男"
    16. p:birthdate-ref="date" />
    17.  
    18. <!-- 通过bean标记来声明这个bean
    19. 使用bean标记的id或name属性来指定bean的名称 ,
    20. bean的class属相来指定bean的类型(具体的不是抽象的,不然需要修改abstract属性为true),
    21. name属性指定setter方法对应的名称,比如setBirthdate就写birthdate,
    22. ref属性用来指定需要引用的那个已经存在的bean的名称-->
    23. <!--Customer c = new Customer(); -->
    24. <!--<bean id="customer" class="ecut.ioc.di.Customer" > c.setId(1002)
    25. <property name="id" value="1002" />
    26. <property name="name" value="张翠山" />
    27. <property name="gender" value="男" />
    28. <property name="birthdate" ref="date" />
    29. </bean>
    30. -->
    31.  
    32. </beans>

    测试类

    1. package ecut.ioc.di;
    2.  
    3. import org.springframework.context.support.AbstractApplicationContext;
    4. import org.springframework.context.support.ClassPathXmlApplicationContext;
    5.  
    6. public class TestSetterInjection {
    7.  
    8. public static void main(String[] args) {
    9. //指定configuration metadata配置元数据
    10. String configLocations = "classpath:ecut/**/di/setter-injection.xml" ;
    11. //创建spring IOC容器
    12. AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations );
    13. //ready for use (此时可以从指定的IOC容器中获取指定名称的bean实例了)
    14. Customer c = container.getBean( "customer" , Customer.class );
    15. //从容器中获取的bean实例中获取属性值
    16. System.out.println( c.getId() );
    17.  
    18. System.out.println( c.getName() );
    19.  
    20. System.out.println( c.getGender() );
    21.  
    22. System.out.println( c.getBirthdate() );
    23. //关闭spring的IOC容器
    24. container.close();
    25.  
    26. }
    27.  
    28. }

3、构造方法注入 : ( constructor-base )通过构造方法往里面传入值

  • 测试案例

    constructor-injection.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2.  
    3. <beans xmlns="http://www.springframework.org/schema/beans"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:p="http://www.springframework.org/schema/p"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
    7.  
    8. <bean id="date" class="java.util.Date" />
    9.  
    10. <!-- constructor 调用有参数的构造 -->
    11. <bean id="customer" class="ecut.ioc.di.Customer" >
    12. <!-- 在bean标记内部使用constructor-arg标记来指定构造方法的参数为 ,
    13. 也可以根据type匹配相应的参数如果这个类型参数唯一,
    14. 也可以指定index来指定参数,若和参数顺序一致index可以省略,
    15. name是指参数名称,value是指定的参数值 -->
    16. <constructor-arg name="id" type="java.lang.Integer" value="1003" />
    17. <constructor-arg name="name" type ="java.lang.String" value="殷素素" />
    18. <constructor-arg name="gender" value="女" />
    19. <!-- 如果参数类型不是简单类型就需要使用ref去引用其他的bean -->
    20. <constructor-arg name="birthdate" ref="date" />
    21. </bean>
    22.  
    23. <!-- <bean id="customer" class="ecut.ioc.di.Customer" >
    24. 在bean标记内部使用constructor-arg标记来指定构造方法的参数为 ,
    25. 也可以根据type匹配相应的参数如果这个类型参数唯一,
    26. 也可以指定index来指定参数,若和参数顺序一致index可以省略,
    27. name是指参数名称,value是指定的参数值
    28. <constructor-arg type="java.lang.Integer" value="1003" />
    29. <constructor-arg type ="java.lang.String" value="殷素素" />
    30. <constructor-arg value="女" />
    31. <constructor-arg ref="date" />
    32. </bean> -->
    33.  
    34. <!-- constructor 调用有参数的构造 -->
    35. <!-- <bean id="customer" class="ecut.ioc.di.Customer" >
    36. 在bean标记内部使用constructor-arg标记来指定构造方法的参数为 ,
    37. 也可以根据type匹配相应的参数如果这个类型参数唯一,
    38. 也可以指定index来指定参数,若和参数顺序一致index可以省略,
    39. name是指参数名称,value是指定的参数值
    40. <constructor-arg name="id" value="1003" />
    41. <constructor-arg name="name" value="殷素素" />
    42. <constructor-arg name="gender" value="女" />
    43. <constructor-arg name="birthdate" ref="date" />
    44. </bean> -->
    45.  
    46. <!-- <bean id="customer" class="ecut.ioc.di.Customer" >
    47. 在bean标记内部使用constructor-arg标记来指定构造方法的参数为 ,
    48. 也可以根据type匹配相应的参数如果这个类型参数唯一,
    49. 也可以指定index来指定参数,若和参数顺序一致index可以省略,
    50. name是指参数名称,value是指定的参数值
    51. <constructor-arg value="1003" />
    52. <constructor-arg value="殷素素" />
    53. <constructor-arg value="女" />
    54. <constructor-arg ref="date" />
    55. </bean> -->
    56.  
    57. <!-- <bean id="customer" class="ecut.ioc.di.Customer" >
    58. 在bean标记内部使用constructor-arg标记来指定构造方法的参数为 ,
    59. 也可以根据type匹配相应的参数如果这个类型参数唯一,
    60. 也可以指定index来指定参数,若和参数顺序一致index可以省略,
    61. name是指参数名称,value是指定的参数值
    62. <constructor-arg index="0" value="1003" />
    63. <constructor-arg index="2" value="女" />
    64. <constructor-arg index="1" value="殷素素" />
    65. <constructor-arg index="3" ref="date" />
    66. </bean> -->
    67.  
    68. </beans>

    在bean标记内部使用constructor-arg标记来指定构造方法的参数为 ,也可以根据type匹配相应的参数如果这个类型参数唯一,也可以指定index来指定参数,若和参数顺序一致index可以省略,name是指参数名称,value是指定的参数值。

    测试类

    1. package ecut.ioc.di;
    2.  
    3. import org.springframework.context.support.AbstractApplicationContext;
    4. import org.springframework.context.support.ClassPathXmlApplicationContext;
    5.  
    6. public class TestConstructorInjection {
    7.  
    8. public static void main(String[] args) {
    9. //指定configuration metadata配置元数据
    10. String configLocations = "classpath:ecut/**/di/constructor-injection.xml" ;
    11. //创建spring IOC容器
    12. AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations );
    13. //ready for use (此时可以从指定的IOC容器中获取指定名称的bean实例了)
    14. Customer c = container.getBean( "customer" , Customer.class );
    15. //从容器中获取的bean实例中获取属性值
    16. System.out.println( c.getId() );
    17.  
    18. System.out.println( c.getName() );
    19.  
    20. System.out.println( c.getGender() );
    21.  
    22. System.out.println( c.getBirthdate() );
    23. //关闭spring的IOC容器
    24. container.close();
    25.  
    26. }
    27.  
    28. }

    如果在Customer类中写了带参数的构造最好写 无参构造,只写有参构造,虚拟机不会再分配无参构造, 因为没有提供无参构造会抛出异常BeanCreationException,没有找到默认构造方法 No default constructor found;

Spring IoC容器注入集合

1、Spring不仅能注入简单类型数据,还能注入集合(Collection、Set、List)类型、数组(Array)类型、字典(Map)类型数据、Properties类型数据。

2、3.0之前的注入方式:在 <property> 标签 内部 使用 set 、list 、map 、props 标签实现注入: normal-injection.xml

  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:util="http://www.springframework.org/schema/util"
  5. xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
  7.  
  8. <bean id="birthdate" class="java.util.Date" />
  9.  
  10. <bean id="findFriend" class="java.util.Date" />
  11.  
  12. <bean id="user" class="ecut.ioc.collection.User">
  13. <property name="id" value="1001" />
  14. <property name="name" value="张三丰" />
  15.  
  16. <!-- 注入 Set 集合 -->
  17. <property name="hobby">
  18. <set>
  19. <value>吃饭</value>
  20. <value>睡觉</value>
  21. <value>泡妞/被泡</value>
  22. </set>
  23. </property>
  24.  
  25. <!-- 注入 List 集合 -->
  26. <property name="luckDay">
  27. <list>
  28. <!-- 如果不是基本数据类型及其包装类型,通过ref 来引用-->
  29. <ref bean="birthdate"/>
  30. <ref bean="findFriend"/>
  31. </list>
  32. </property>
  33.  
  34. <!-- 注入 Map 集合 -->
  35. <property name="score">
  36. <map>
  37. <entry key="前端" value="45" />
  38. <entry key="后端" value="54" />
  39. </map>
  40. </property>
  41.  
  42. <!-- 注入 Properties 集合 -->
  43. <property name="address">
  44. <props>
  45. <prop key="软件楼">东理北门内,太谷路旁,软件楼一楼厕所边的那个门</prop>
  46. <prop key="宿舍">东理南区研一好望角</prop>
  47. </props>
  48. </property>
  49.  
  50. </bean>
  51.  
  52. </beans>

3、3.0全新的注入方式:使用 Spring 3.0 开始提供的 util 命名空间,结合 <property> 或 p 命名空间实现注入: util-injection.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:util="http://www.springframework.org/schema/util"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  7. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
  8.  
  9. <bean id="birthdate" class="java.util.Date" />
  10.  
  11. <bean id="findFriend" class="java.util.Date" />
  12.  
  13. <util:set id="hobby" set-class="java.util.HashSet">
  14. <value>吃饭</value>
  15. <value>睡觉</value>
  16. <value>泡妞/被泡</value>
  17. </util:set>
  18.  
  19. <util:list id="luckDay" list-class="java.util.ArrayList">
  20. <ref bean="birthdate" />
  21. <ref bean="findFriend" />
  22. </util:list>
  23.  
  24. <util:map id="score" map-class="java.util.HashMap" key-type="java.lang.String" value-type="java.lang.Integer">
  25. <entry key="前端" value="45" />
  26. <entry key="后端" value="54" />
  27. </util:map>
  28.  
  29. <util:properties id="address">
  30. <prop key="软件楼">东理北门内,太谷路旁,软件楼一楼厕所边的那个门</prop>
  31. <prop key="宿舍">东理南区研一好望角</prop>
  32. </util:properties>
  33.  
  34. <!-- <bean id="user" class="io.spring.ioc.collection.User" p:address-ref="address"> -->
  35. <bean id="user" class="ecut.ioc.collection.User" >
  36. <property name="id" value="1001" />
  37. <property name="name" value="张三丰" />
  38.  
  39. <!-- 注入 Set 集合 -->
  40. <property name="hobby" ref="hobby"/>
  41.  
  42. <!-- 注入 List 集合 -->
  43. <property name="luckDay" ref="luckDay" />
  44.  
  45. <!-- 注入 Map 集合 -->
  46. <property name="score" ref="score"/>
  47.  
  48. <!-- 注入 Properties 集合 -->
  49. <property name="address" ref="address"/>
  50.  
  51. </bean>
  52.  
  53. </beans>

转载请于明显处标明出处

https://www.cnblogs.com/AmyZheng/p/9249548.html

Spring学习(三)的更多相关文章

  1. spring学习(三) ———— spring事务操作

    前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...

  2. Spring学习三

    Spring注解来注入bean 在classpath中扫描组件 组件扫描,即componetscanning 利用注解来扫描的组件有  @Component  :基本注解,表示一个受Spring管理的 ...

  3. Spring学习(三)--高级装配

    一.Spring profile 在开发软件的时候,有一个很大的挑战就是将应用程序从一个环境迁 移到另外一个环境.开发阶段中,某些环境相关做法可能并不适合迁 移到生产环境中,甚至即便迁移过去也无法正常 ...

  4. spring学习 三 框架的搭建

    1 导入jar包 spring启来最少要5个包,四个核心包和一个依赖的日志包 2 创建配置文件 在dynamic web project下的src目录下,创建一个spring的xml配置文件,名称可以 ...

  5. Spring学习(三)-----Spring自动装配Beans

    在Spring框架,可以用 auto-wiring 功能会自动装配Bean.要启用它,只需要在 <bean>定义“autowire”属性. <bean id="custom ...

  6. Spring学习三----------注入方式

    © 版权声明:本文为博主原创文章,转载请注明出处 Spring注入方式 本篇博客只讲最常用的两种注入方式:设值注入和构造器注入.代码为完整代码,复制即可使用. 1.目录结构 2.pom.xml < ...

  7. spring学习三:Spring Bean 生命周期

    Bean 的生命周期 理解 Spring bean 的生命周期很容易.当一个 bean 被实例化时,它可能需要执行一些初始化使它转换成可用状态.同样,当 bean 不再需要,并且从容器中移除时,可能需 ...

  8. spring学习三:Spring的Aop、代理

    ref:https://mp.weixin.qq.com/s/J77asUvw8FcnF-6YlX6AAw AOP相关术语:    Joinpoint(连接点):类里面可以被增强的方法,这些方法称为连 ...

  9. Spring学习(六)-----Spring使用@Autowired注解自动装配

    Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...

随机推荐

  1. 单例模式的Java泛型实现方式

    import java.util.HashMap; import java.util.Map; /** * Created by zhao.wu on 2016/11/18. */ public cl ...

  2. 【代码学习】PYTHON 私有化

    一.私有化 xx: 公有变量_x: 单前置下划线,私有化属性或方法,from somemodule import *禁止导入,类对象和子类可以访问__xx:双前置下划线,避免与子类中的属性命名冲突,无 ...

  3. Apache的虚拟主机功能(基于IP地址、基于虚拟主机、基于端口)

    1. 安装Apache服务程序(系统用户,1-199之间) 第一步:在虚拟机软件里选中光盘镜像: 第二步:将光盘设备挂载到/media/cdrom目录 输入:mkdir -p /media/cdrom ...

  4. Node.js Learning Notes

    简介 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事件驱动I/O服务 ...

  5. git 工具常见命令

    1.git是什么 git是分布式版本管理工具,一台电脑既可以是客户端,也可以是服务端.工作过程中可以断开网络. git中的三个概念: 1.版本库:在初始化git版本库之后会生成一个隐藏的文件, .gi ...

  6. 传奇GOM引擎授权过期解决方法.

    传奇GOM引擎授权过期解决方法 下载最新的GOM引擎,将里面的Key.Lic文件找出来,替换掉授权过期的版本,如果你本身是免费版最好是找同样的免费版的来覆盖. 如果你本身是免费版,但是却用的是商业版K ...

  7. python中,字符串前的u,b,r字符的含义

    1.字符串前加 u 例:u"我是含有中文字符组成的字符串." 作用: 后面字符串以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时 ...

  8. python 切片技巧

    说明: 字符串[开始索引:结束索引:步长] 开始索引:从指定位置开始截取: 结束索引:从指定位置结束截取,但不包含该位置的字符. 步长:不指定时步长为1: 1)当步长为正数时候,那么切片是从左到右进行 ...

  9. windows 配置hadoop环境

    在idea运行spark程序的时候报错:java.io.IOException: Could not locate executable null\bin\winutils.exe in the Ha ...

  10. hash路由

    class HashRouter{ constructor(){ //用于存储不同hash值对应的回调函数 this.routers = {}; window.addEventListener('ha ...