spring3——IOC之基于XML的依赖注入(DI )
2017-02-21 23:56
162 查看
我们知道spring容器的作用是负责对象的创建和对象间关系的维护,在上一篇博客中我们讲到spring容器会先调用对象的无参构造方法创建一个空值对象,那么接下来容器就会对对象的属性进行初始化,这个初始化的过程就叫“依赖注入”。spring容器对属性进行初始化的方式有很多,但是最常用的是"设置注入”,就是说spring容器会调用setter方法对属性进行初始化。
这篇博客会重点讲述基于XML实现依赖注入,至于基于注解实现的依赖注入,会在以后的篇幅中进行讲述分析。
spring容器对于对象属性的注入可以分为两种:
(1)属性是String类型或者是基本数据类型的。
(2)属性是java对象的。
下面我们通过实例来说明spring容器基于XML实现这两种方式的依赖注入。
一,属性是String类型或者是基本数据类型的。
Student类:
XML文件:
测试类:
XML文件中spring容器通过“<propertyname="name"value="张三"></property>”标签调用了Student对象的"setName"方法对属性的值进行了注入。所以说基于XML实现依赖注入时,属性的set方法不可缺少。
二、属性是java对象的。
我们再来准备一个Teacher类。使用基于XML的依赖注入实现对Student属性的注入。
XML文件:
测试类:
我们来看一下这个依赖注入的过程:
(1):spring容器调用Student类的Teacher类的无参构造方法,创建空值对象。
(2):spring容器调用Student对象的set方法完成对属性的初始化操作(依赖注入)。
(3):spring容器调用Teacher对象的set方法完成对Student属性的初始化操作(依赖注入)。
另外对于基于XML实现java对象的依赖注入,bean标签还提供了一个属性:autowire="byName"或autowire="byType"允许我们在XML文件中这样写。
这样写表示容器所有bean属性的注入都是按名称来注入的,若是使用default-autowire="byType",则表示按类型注入,使用它们的注意事项同上。
最后说一点,我们作为程序员,研究问题还是要仔细深入一点的。当你对原理了解的有够透彻,开发起来也就得心应手了,很多开发中的问题和疑惑也就迎刃而解了,而且在面对其他问题的时候也可做到触类旁通。当然在开发中没有太多的时间让你去研究原理,开发中要以实现功能为前提,可等项目上线的后,你有大把的时间或者空余的时间,你大可去刨根问底,深入的去研究一项技术,为觉得这对一名程序员的成长是很重要的事情。
这篇博客会重点讲述基于XML实现依赖注入,至于基于注解实现的依赖注入,会在以后的篇幅中进行讲述分析。
spring容器对于对象属性的注入可以分为两种:
(1)属性是String类型或者是基本数据类型的。
(2)属性是java对象的。
下面我们通过实例来说明spring容器基于XML实现这两种方式的依赖注入。
一,属性是String类型或者是基本数据类型的。
Student类:
publicclassStudent{ privateStringname; publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } }
XML文件:
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <beanid="student"class="com.opensource.bean.Student"> <propertyname="name"value="张三"></property> </bean> </beans>
测试类:
publicclassMyTest{ publicstaticvoidmain(String[]args){ ApplicationContextac=newClassPathXmlApplicationContext("spring-bean.xml"); Studentstudent=(Student)ac.getBean("student"); System.out.println(student.getName()); } }
XML文件中spring容器通过“<propertyname="name"value="张三"></property>”标签调用了Student对象的"setName"方法对属性的值进行了注入。所以说基于XML实现依赖注入时,属性的set方法不可缺少。
二、属性是java对象的。
我们再来准备一个Teacher类。使用基于XML的依赖注入实现对Student属性的注入。
publicclassTeacher{ privateStudentstudent; publicStudentgetStudent(){ returnstudent; } publicvoidsetStudent(Studentstudent){ this.student=student; } }
XML文件:
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <beanid="student"class="com.opensource.bean.Student"> <propertyname="name"value="张三"></property> </bean> <beanid="teacher"class="com.opensource.bean.Teacher"> <propertyname="student"ref="student"></property> </bean> </beans>
测试类:
publicclassMyTest{ publicstaticvoidmain(String[]args){ ApplicationContextac=newClassPathXmlApplicationContext("spring-bean.xml"); Teacherteacher=(Teacher)ac.getBean("teacher"); System.out.println(teacher.getStudent().getName()); } }
我们来看一下这个依赖注入的过程:
(1):spring容器调用Student类的Teacher类的无参构造方法,创建空值对象。
(2):spring容器调用Student对象的set方法完成对属性的初始化操作(依赖注入)。
(3):spring容器调用Teacher对象的set方法完成对Student属性的初始化操作(依赖注入)。
另外对于基于XML实现java对象的依赖注入,bean标签还提供了一个属性:autowire="byName"或autowire="byType"允许我们在XML文件中这样写。
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <beanid="student"class="com.opensource.bean.Student"> <propertyname="name"value="张三"></property> </bean> <beanid="teacher"class="com.opensource.bean.Teacher"autowire="byName"> </bean> //注意在xml文件中id不可重复,这里是为了说明问题。 <beanid="teacher"class="com.opensource.bean.Teacher"autowire="byType"> </bean> </beans>
使用autowire="byName"时应当注意的是:Teacher类的Student属性名必须与XML文件中用于创建Student对象<bean>标签的id的值保持一致。而使用autowire="byType"则没有这种限制, Teacher类的Student属性名可以按照java对变量的命名规则任意命名,但要保证的是容器中只有一个Student类型的对象存在。 对于autowire="byName"或autowire="byType"在XML中还可以在顶级标签<beans>中进行全局的设置。
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"default-autowire="byName"> <beanid="student"class="com.opensource.bean.Student"> <propertyname="name"value="张三"></property> </bean> <beanid="teacher"class="com.opensource.bean.Teacher"> </bean> </beans>
这样写表示容器所有bean属性的注入都是按名称来注入的,若是使用default-autowire="byType",则表示按类型注入,使用它们的注意事项同上。
最后说一点,我们作为程序员,研究问题还是要仔细深入一点的。当你对原理了解的有够透彻,开发起来也就得心应手了,很多开发中的问题和疑惑也就迎刃而解了,而且在面对其他问题的时候也可做到触类旁通。当然在开发中没有太多的时间让你去研究原理,开发中要以实现功能为前提,可等项目上线的后,你有大把的时间或者空余的时间,你大可去刨根问底,深入的去研究一项技术,为觉得这对一名程序员的成长是很重要的事情。
相关文章推荐
- spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- 【JavaWeb-23】spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- Spring--IoC--基于XML的DI-自动注入-byName
- Spring--IoC--基于XML的DI-SPEL注入
- Spring--IoC--基于XML的DI-自动注入-byType
- (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)
- Spring的IOC和依赖注入DI理解
- Spring.net 控制反转(IOC)依赖注入(DI)的使用 以及Config文件的配置
- Spring环境搭建之:控制反转(IoC Inversion of Control)与依赖注入(DI Depenency Injection)
- Spring学习3—控制反转(IOC)基于Annotation(注解)的依赖注入实现
- Spring 4 学习笔记2:控制反转(IoC)和依赖注入(DI)
- Helloworld之Spring依赖注入/控制反转(DI/IoC)版
- Spring控制反转(IOC)和依赖注入(DI),再记不住就去出家!
- Spring入门--控制反转(IOC)与依赖注入(DI)
- Spring学习(三)IOC控制反转与DI依赖注入
- Spring IOC 依赖注入的两种方式XML和注解
- Spring的依赖注入(基于XML)
- spring的依赖注入DI(IOC)
- 使用XPath解析xml实现简单的Spring IOC完成bean的依赖注入
- Spring的Ioc和DI依赖注入的理解