基于XML的DI(依赖注入):

  Bean 实例在调用无参构造器创建了空值对象后,就要对 Bean 对象的属性进行初始化。初始化是由容器自动完成的,称为注入。根据注入方式的不同,常用的有两类:设值注入、构造注入。还有另外一种,实现特定接口注入。由于这种方式采用侵入式编程,污染了代码,所以几乎不用。

  注入分类:

    (1) 设值注入:

      设值注入是指,通过 setXXX() 方法传入被调用者的实例。这种注入方式简单、直观,因而在 Spring 的依赖注入中大量使用。

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="mySchool" class="com.tongji.di01.School">
<property name="name" value="清华大学"/>
</bean>
<bean id="student" class="com.tongji.di01.Student">
<property name="name" value="张三"/>
<property name="age" value="23"/>
<property name="school" ref="mySchool"/>
</bean>
</beans>

    (2) 构造注入:

      构造注入是指,在构造调用者实例的同时,完成被调用者的实例化。即,使用构造器设置依赖关系。

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="mySchool" class="com.tongji.di02.School">
<property name="name" value="清华大学"/>
</bean>
<bean id="student" class="com.tongji.di02.Student">
<constructor-arg name="name" value="李四"/>
<constructor-arg name="age" value="24"/>
<constructor-arg name="school" ref="mySchool"/> <!--<constructor-arg index="0" value="李四"/>
<constructor-arg index="1" value="24"/>
<constructor-arg index="2" ref="mySchool"/>
--> <!--<constructor-arg value="李四"/>
<constructor-arg value="24"/>
<constructor-arg ref="mySchool"/>
--> </bean>
</beans>

  

  命名空间注入:

    对于设值注入与构造注入,在配置文件中,除了使用<property/>或<constructor-arg/>标签外,还可使用命名空间注入的方式,让注入的值以<bean/>标签属性的方式出现。根据注入实现方式的不同,分为 p 命名空间注入与 c 命名空间注入。
       p 命名空间注入:采用设值注入方式,故需要有相应的 setXXX()
       c 命名空间注入:采用构造注入方式,故需要有相应的构造器

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="mySchool" class="com.tongji.di03.School" p:name="北京大学"/> <bean id="student" class="com.tongji.di03.Student" p:name="王五" p:age="25" p:school-ref="mySchool"/> </beans>
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="mySchool" class="com.tongji.di04.School">
<property name="name" value="清华大学"/>
</bean>
<bean id="student" class="com.tongji.di04.Student" c:name="赵六" c:age="26" c:school-ref="mySchool"/> </beans>

    注意添加的约束:xmlns:p="http://www.springframework.org/schema/p" 和 xmlns:c="http://www.springframework.org/schema/c"

 

  集合属性的注入:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="mySchool1" class="com.tongji.di05.School">
<property name="name" value="清华大学"/>
</bean>
<bean id="mySchool2" class="com.tongji.di05.School">
<property name="name" value="北京大学"/>
</bean> <bean id="some" class="com.tongji.di05.Some">
<property name="names">
<array>
<value>张三</value>
<value>赵四</value>
<value>王五</value>
</array>
</property>
<property name="schools">
<list>
<ref bean="mySchool1"/>
<ref bean="mySchool2"/>
</list>
</property>
<property name="mySets">
<set>
<value>中国</value>
<value>上海</value>
<value>黄渡理工大学</value>
</set>
</property>
<property name="myMap">
<map>
<entry key="QQ" value="7654321"/>
<entry key="Email" value="7654321@qq.com"/>
</map>
</property>
<property name="myProes">
<props>
<prop key="address">上海曹安公路</prop>
<prop key="phone">123456778</prop>
</props>
</property>
</bean>
</beans>

  对域属性的自动注入:

    对于域属性的注入,也可不在配置文件中显示的注入。可以通过为<bean/>标签设置autowire 属性值,为域属性进行隐式自动注入。根据自动注入判断标准的不同,可以分为两种:
       byName:根据名称自动注入
       byType:根据类型自动注入
    byName:当配置文件中被调用者 Bean 的 id 值与代码中调用者 Bean 类的属性名相同时,可使用byName 方式,让容器自动将被调用者 Bean 注入给调用者 Bean。容器是通过调用者的 Bean类的属性名与配置文件的被调用者 bean 的 id 进行比较而实现自动注入的。

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="school" class="com.tongji.di06.School">
<property name="name" value="清华大学"/>
</bean> <!-- byName方式域属性自动注入,要求自动注入的bean的id与被注入的属性名相同 -->
<bean id="student" class="com.tongji.di06.Student" autowire="byName">
<property name="name" value="张三"/>
<property name="age" value="23"/>
</bean>
</beans>

    byType:使用 byType 方式自动注入,要求:配置文件中被调用者 bean 的 class 属性指定的类,要与代码中调用者 Bean 类的某域属性类型同源。即要么相同,要么有 is-a 关系(子类,或是实现类)。但这样的同源的被调用 bean 只能有一个。多于一个,容器就不知该匹配哪一个了。

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="mySchool" class="com.tongji.di07.School">
<property name="name" value="清华大学"/>
</bean> <!-- byType方式域属性自动注入,要求容器中与被注入属性类型具有is-a关系的Bean,只能有一个 -->
<bean id="student" class="com.tongji.di07.Student" autowire="byType">
<property name="name" value="张三"/>
<property name="age" value="23"/>
</bean>
</beans>

  

  使用SPEL表达式注入:(需要进一步学习SPEL表达式)

    SPEL,Spring Expression Language,即 Spring EL 表达式语言。即,在 Spring 配置文件中为 Bean 的属性注入值时,可直接使用 SPEL 表达式计算的结果。SPEL 表达式以#开头,后跟一对大括号。用法:  <bean  id=“abc”   value=“#{…}”/>。

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.tongji.di08.Person">
<property name="pname" value="李四"/>
<property name="page" value="#{T(java.lang.Math).random() * 80}"/>
</bean> <bean id="student" class="com.tongji.di08.Student">
<property name="name" value="#{person.pname}"/>
<property name="age" value="#{person.computeAge()}"/>
</bean>
</beans>

  

  使用内部 Bean 注入 :
    若不希望代码直接访问某个 bean,即,在代码中通过 getBean 方法获取该 Bean 实例, 则可将该 Bean 的定义放入调用者 bean 定义的内部

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--
<bean id="mySchool" class="com.tongji.di09.School">
<property name="name" value="清华大学"/>
</bean>
--> <bean id="student" class="com.tongji.di09.Student">
<property name="name" value="张三"/>
<property name="age" value="23"/>
<property name="school">
<bean class="com.tongji.di09.School">
<property name="name" value="清华大学"/>
</bean>
</property>
</bean>
</beans>

  使用同类抽象 Bean 注入:
    当若干 Bean 实例同属于一个类,且这些实例的属性值又有相同值时,可以使用抽象Bean,以简化配置文件。
    抽象 Bean 是用于让其它 bean 继承的。这个 bean 在 Bean 类中是不能通过 getBean 方法获取的。设置 abstract 属性为 true 来指明该 bean 为抽象 bean,默认值为 false。不过,该 bean 不为抽象 bean 时,也可被继承。只不过,在应用中,用于被继承的 bean 一般为抽象 bean。

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 同类抽象Bean -->
<bean id="baseStudent" class="com.tongji.di10.Student" abstract="true">
<property name="school" value="清华大学"/>
<property name="department" value="计算机学院"/>
</bean> <bean id="student1" parent="baseStudent">
<property name="name" value="张三"/>
<property name="age" value="23"/>
</bean>
<bean id="student2" parent="baseStudent">
<property name="name" value="李四"/>
<property name="age" value="24"/>
</bean>
<bean id="student3" parent="baseStudent">
<property name="name" value="王五"/>
<property name="age" value="25"/>
</bean>
</beans>

  使用异类抽象 Bean 注入:

    当若干不同的类对象具有相同的属性,且其值也相同时,可使用异类抽象 Bean。

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 异类抽象Bean -->
<bean id="Person" abstract="true">
<property name="school" value="清华大学"/>
<property name="department" value="计算机学院"/>
</bean> <bean id="student" class="com.tongji.di11.Student" parent="Person">
<property name="name" value="张三"/>
<property name="age" value="23"/>
</bean> <bean id="teacher" class="com.tongji.di11.Teacher" parent="Person">
<property name="name" value="李四"/>
<property name="workAge" value="24"/>
</bean>
</beans>

  为应用指定多个 Spring 配置文件:

    在实际应用里,随着应用规模的增加,系统中 Bean 数量也大量增加,导致配置文件变得非常庞大、臃肿。为了避免这种情况的产生,提高配置文件的可读性与可维护性,可以将 Spring 配置文件分解成多个配置文件。

    (1) 以配置文件数组方式实现:将所有配置文件的路径定义为一个 String 数组,并将其作为容器初始化参数出现。其将与可变参的容器构造器匹配。

 package com.tongji.di12;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test
public void test01() {
//创建容器
String resource1 = "com/tongji/di12/spring-base.xml";
String resource2 = "com/tongji/di12/spring-beans.xml"; String[] resources = {resource1, resource2};
@SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext(resources); Student student = (Student) ac.getBean("student");
System.out.println(student); Teacher teacher = (Teacher) ac.getBean("teacher");
System.out.println(teacher);
} }

    第12、13行的配置文件为并列关系,不分主次。

    (2) 以父子配置文件方式实现 :
      各配置文件中有一个总文件,总配置文件将各其它子文件通过<import/>引入。在 Java代码中只需要使用总配置文件对容器进行初始化即可。

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="classpath:com/tongji/di13/spring-base.xml"/> <!-- 注意classpath -->
<import resource="classpath:com/tongji/di13/spring-beans.xml"/> <!-- 也可使用通配符*。但,此时要求父配置文件名不能满足*所能匹配的格式,否则将出现循环递归包含 --> </beans>
 package com.tongji.di13;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test
public void test01() {
//创建容器
String resource = "com/tongji/di13/applicationContext.xml"; @SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext(resource); Student student = (Student) ac.getBean("student");
System.out.println(student); Teacher teacher = (Teacher) ac.getBean("teacher");
System.out.println(teacher);
} }

Spring4笔记4--基于XML的DI(依赖注入)的更多相关文章

  1. 三大框架 之 Spring(IOC控制反转、DI依赖注入)

    目录 常用词汇 left join与left outer join的区别 Struts2的标签库导入 Spring Spring概述 什么是Spring spring特点 下载 IOC 什么IOC 传 ...

  2. Spring:(二)DI依赖注入方式

    DI 依赖注入 DI(Dependency Injection)依赖注入,说简单一点就将类里面的属性在创建类的过程中给属性赋值,即将对象依赖属性(简单值,集合,对象)通过配置设值给该对象. 属性注入的 ...

  3. Java开发学习(六)----DI依赖注入之setter及构造器注入解析

    一.DI依赖注入 首先来介绍下Spring中有哪些注入方式? 我们先来思考 向一个类中传递数据的方式有几种? 普通方法(set方法) 构造方法 依赖注入描述了在容器中建立bean与bean之间的依赖关 ...

  4. 初识Spring框架实现IOC和DI(依赖注入)

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的, IoC是 ...

  5. 谈谈php里的IOC控制反转,DI依赖注入

    理论 发现问题 在深入细节之前,需要确保我们理解"IOC控制反转"和"DI依赖注入"是什么,能够解决什么问题,这些在维基百科中有非常清晰的说明. 控制反转(In ...

  6. Spring详解(三)------DI依赖注入

    上一篇博客我们主要讲解了IOC控制反转,也就是说IOC 让程序员不在关注怎么去创建对象,而是关注与对象创建之后的操作,把对象的创建.初始化.销毁等工作交给spring容器来做.那么创建对象的时候,有可 ...

  7. 一) Spring 介绍、IOC控制反转思想与DI依赖注入

    一.spring介绍1.IOC反转控制思想(Inversion of Control)与DI依赖注入(Dependency Injection)2.AOP面向切面的编程思想与动态代理3.作用:项目的粘 ...

  8. JAVAWEB 一一 Spirng(框架,IOC控制反转,DI依赖注入)

    jar包 applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <bea ...

  9. AutoFac IoC DI 依赖注入

    AutoFac IoC DI 依赖注入 记录点点滴滴知识,为了更好的服务后来者! 一.为什么使用AutoFac? 之前介绍了Unity和Ninject两个IOC容器,但是发现园子里用AutoFac的貌 ...

随机推荐

  1. BZOJ5288 HNOI/AHOI2018游戏

    首先将之间没有锁的房间合并.显然可达性具有传递性和反交换律(即若a能到达b,则b不能到达a). 考虑对每个房间找到其左右第一个(即与其最接近的)能作为起点到达它的房间.如果能求出这个,对此建两棵树,问 ...

  2. 数据库事物 jdbc事物 spring事物 隔离级别:脏幻不可重复读

    1.数据库事物: 事物的概念 a给b打100块钱的例子 2.jdbc事物: 通过下面代码实现 private Connection conn = null; private PreparedState ...

  3. How Many Points? LightOJ - 1077(线段经过整点个数与gcd 证明)

    题意: 已知两点 (x1,y1) 和 (x2, y2)求两点间线段上的整点的个数 解析: 就是求gcd(abs(x2- x1),abs(y2 - y1)) 证明: 我们分水平方向和竖直方向两个方向看 ...

  4. oracle 存储过程创建报错 Procedure created with compilation errors

    出现这错误的话,存储过程还是会成功创建的,创建好后再逐个打开查找存储过程的问题 问题:基本上就是存储过程里面的表不存在,dblink 不存在    ,用户名.xx表  要么用户名不存在要么表不存在 创 ...

  5. linq 获取实体列表中的某个字段返回ilist<string>

     var list = list.Select(t => t.Field<string>("列名")).ToList();  var list = list.Se ...

  6. 【刷题】BZOJ 3144 [Hnoi2013]切糕

    Description Input 第一行是三个正整数P,Q,R,表示切糕的长P. 宽Q.高R.第二行有一个非负整数D,表示光滑性要求.接下来是R个P行Q列的矩阵,第z个 矩阵的第x行第y列是v(x, ...

  7. MugLife app是一款可以将静态照片变成3D动画的手机应用

    MugLife app是一款可以将静态照片变成3D动画的手机应用,如下效果图所示: 大家可以看到,这个静态图具有了类3D的动画特效,是不是很好玩? 这种算法是如何实现的呢? 这里给出一篇论文“Brin ...

  8. BZOJ5389 比例查询 【离线】

    题目链接 BZOJ5389 题解 太\(sb\)了,这种题都想不出来 发现复杂度允许\(n\sqrt{n}\),我们可以对于每个位置\(\sqrt{n}\)枚举约数,然后维护比例的最晚出现的位置,维护 ...

  9. linux内核分析(网课期末&地面课期中)

    堆栈变化过程: Linux内核分析——计算机是如何工作的 计算机是如何工作的?(总结)——三个法宝 存储程序计算机工作模型,计算机系统最最基础性的逻辑结构: 函数调用堆栈,高级语言得以运行的基础,只有 ...

  10. C++接口继承与实现继承的区别和选择

    1.接口继承与实现继承的区别 <Effective C++>条款三十四:区分接口继承和实现继承中介绍的比较啰嗦,概括地说需要理解三点: (1)纯虚函数只提供接口继承,但可以被实现: (2) ...