Spring学习(一)加强 DI依赖注入的小案例
2017-03-01 11:44
417 查看
依赖注入,就是一个类依靠另一个类实现,
举个例子,比如说,创建一个A类。在创建一个B类,当然创建过程,Spring已经帮我们完成了,这时候在A类中建一个B类的私有成员变量set出来,在xml文件中 配置属性标签,在另外的测试类C中通过bean的工厂就能获取到B类中的方法,但是这里注意了,在测试C类中,并没有通过xml文件获取B类的实例。这就是依赖注入,有点绕口,就是B依赖A注入到C的调用中。(可能解释的不是很精准)
接下来,我们把代码贴出来,供大家参考:
B类也就是我要创建的Student类:
A类,我创建的Person类:
再来看看配置文件:
接下来就是测试C类了:
这片就简单讲解到这里。
举个例子,比如说,创建一个A类。在创建一个B类,当然创建过程,Spring已经帮我们完成了,这时候在A类中建一个B类的私有成员变量set出来,在xml文件中 配置属性标签,在另外的测试类C中通过bean的工厂就能获取到B类中的方法,但是这里注意了,在测试C类中,并没有通过xml文件获取B类的实例。这就是依赖注入,有点绕口,就是B依赖A注入到C的调用中。(可能解释的不是很精准)
接下来,我们把代码贴出来,供大家参考:
B类也就是我要创建的Student类:
package com.fly.spring; public class Student { private String sex; public void getSex() { System.out.println("******hello word*******" + sex); } public void setSex(String sex) { this.sex = sex; } }
A类,我创建的Person类:
package com.fly.spring; public class Person { private String name; public void getName() { student.getSex(); System.out.println("hello" + name); } public void setName(String name) { this.name = name; } // *******Student依赖Person注入的写法****************************************************************** private Student student; public void setStudent(Student student) { this.student = student; } }
再来看看配置文件:
<?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"> <!-- 模拟spring执行过程 创建Person实例:Person person = new Person() IoC <bean> 创建dao实例:Student student = new Student() IoC 将dao设置给person:person.setStudent(student); DI <property> <property> 用于进行属性注入 name: bean的属性名,通过setter方法获得 setStudent ##> Student ##> student ref :另一个bean的id值的引用 --> <!-- 创建Person实例 --> <bean id="person" class="com.fly.spring.Person"> <property name="student" ref="studentID"></property> </bean> <!-- 创建Student实例 --> <bean id="studentID" class="com.fly.spring.Student"></bean> </beans>
接下来就是测试C类了:
package com.fly.spring; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; /** * 演示Spring的依赖注入 * @author Administrator * */ public class DiSpring { @Test // IOC public void demo(){ String xmlPath = "com/fly/spring/applicationContext.xml"; ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath); Person person = (Person) context.getBean("person"); person.getName(); } @Test // DI public void demoDi(){ String xmlPath = "com/fly/spring/applicationContext.xml"; BeanFactory factory = new XmlBeanFactory(new ClassPathResource(xmlPath)); Person bean = (Person) factory.getBean("person"); bean.getName(); } }
这片就简单讲解到这里。
相关文章推荐
- Spring学习(1):控制反转(IoC)和依赖注入(DI)的详解以及注解(annotation)开发入门案例
- spring学习总结(三):IOC & DI 配置 Bean 之配置形式及依赖注入方式
- spring学习笔记 -- day03 IOC中的bean标签、DI依赖注入
- Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea
- Spring学习笔记[3] -- DI依赖注入
- Spring -Spring的 DI - 依赖注入的 使用学习
- Spring 4 学习笔记2:控制反转(IoC)和依赖注入(DI)
- Spring 学习笔记(4)—— Bean 的基本配置、依赖注入(DI)、注入参数引用其他Bean
- Spring 4 学习笔记3:依赖注入(DI)
- Spring学习四:依赖注入DI(Dependency Injection)
- Spring学习之——控制反转(IoC)与依赖注入(DI)
- Spring学习,依赖注入(DI)、控制反转(IoC)和面向切面(AOP)
- Spring学习--DI依赖注入
- spring 依赖注入DI-个人学习笔记
- Spring 学习之 DI 依赖注入
- Spring学习(一)---依赖注入(DI)1
- Spring学习(三)IOC控制反转与DI依赖注入
- SPRING.NET 1.3.2 学习5--依赖注入
- Spring2.5.6学习笔记-DI的两种注入方式
- Spring -- 控制反转IoC/依赖注入DI的实现