依赖注入的三种方式


进行依赖注入有三种方式:

1、构造方法依赖注入

public class StupidStudent { private SmartStudent smartStudent; public StupidStudent(SmartStudent smartStudent) { this.smartStudent = smartStudent; } public doHomewrok() { smartStudent.doHomework(); System.out.println("学渣抄作业"); } }

 

public class StudentTest { public static void main(String[] args) { SmartStudent smartStudent = new SmartStudent(); StupidStudent stupidStudent = new StupidStudent(smartStudent); stupidStudent.doHomework(); } }

这种方式好比学渣从一开始就赖上了一个学霸,并且和这个学霸建立了长期合作关系。

2、setter方法注入

public class StupidStudent { private SmartStudent smartStudent; public void setSmartStudent(SmartStudent smartStudent) { this.smartStudent = smartStudent; } public doHomewrok() { smartStudent.doHomework(); System.out.println("学渣抄作业"); } }

 

public class StudentTest { public static void main(String[] args) { SmartStudent smartStudent = new SmartStudent(); StupidStudent stupidStudent = new StupidStudent(); stupidStudent.setSmartStudent(smartStudent); stupidStudent.doHomework(); } }

这种方式学霸和学渣只是暂时的合作关系,如果学渣赖上了另一个学霸(调用set()方法传入了另一个对象),那么学渣和学霸的合作关系就结束了。

3、接口注入

public class StupidStudent { public void doHomewrok(SmartStudent smartStudent) { smartStudent.doHomework(); System.out.println("学渣抄作业"); } }

 

public class StudentTest { public static void main(String[] args) { SmartStudent smartStudent = new SmartStudent(); StupidStudent stupidStudent = new StupidStudent(); stupidStudent.doHomework(smartStudent); } }

采用这种注入方式,学渣只是在做作业时,才临时抱佛脚地找一下学霸。 

 

 

一:spring容器

1.在spring IOC容器读取bean配置创建bean实例之前,必须对他进行实例化,只有在容器实例化之后,才可以在IOC容器里获取实例并使用。

  1.  
    //创建spring容器对象,applicationContext是IOC容器,实际上也是一个接口
  2.  
    //ClassPathXmlApplicationContext:是ApplicationContext的接口实现类,是在类的路径下实现该类
  3.  
    ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml");

2.spring提供了两种类型的IOC容器实现

  • BeanFactory:IOC容器的基本实现。
  • ApplicationContext:提供了更多的高级特性,是BeanFactory的子类。
  • BeanFactory是spring框架的基础设施,面向spring本身,而ApplicationContext面向使用spring框架的开发中,几乎所有的应用场合都是使用ApplicationContext,而非BeanFactory。
  • 无论使用何种方式,配置文件时是相同的。

二:ApplicationContext

1.ApplicationContext是一个接口,其有两个主要实现类

  • ClassPathXmlApplicationContext:从类的路径下加载配置文件。
  • FileSystemXmlApplicationContext:从文件系统中加载配置文件

2.configurableApplicationContext扩展于ApplicationContext,新增加两个主要方法,refresh()方法,close()方法。让ApplicationContext具有启动,刷新和关闭上下文的能力。

3.ApplicationContext在初始上下文的时候,就实例化所有单例的bean

4.WebApplication是专门用来为WEB应用而准备的,他允许从相对于WEB根目录的路径中完成初始操作。

三:依赖 注入的三种方式

1.spring支持三种依赖注入的方式

  • 属性注入(最常用的)
  • 构造器注入
  • 工厂方法注入(很少使用,不推荐大家使用,这里就不介绍了)

2.属性注入

  • 属性注入即通过set方法注入Bean的属性值或依赖的对象
  • 属性注入使用<property>元素,使用name属性指定Bean的属性名称,value属性或者value子节点指定属性值
  • 属性注入是事件开发中最常用的注入方式
  1.  
    <!--
  2.  
    id:我们从IOC容器取出的唯一标识,一般是类名的首字母小写
  3.  
    class:bean的全类名,通过反射的方式来创建bean,所以要求bean必须有无参的构造器
  4.  
    property:代表属性的配置,其中name是调用name的set方法(注意set方法的名字要保持一致),value是赋值
  5.  
    -->
  6.  
    <bean id="helloWorld" class="com.dhx.HelloWorld">
  7.  
    <property name="name" value="shangguigu"></property>
  8.  
    </bean>

3.构造器方法注入

  •  通过构造方法注入bean的属性值或依赖的对象,他保证了Bean实例在实例化就可以使用
  • 构造器注入在<constructor-arg>元素里声明属性,<constructor-arg>中没有name属性
  • 使用构造器注入属性值可以指定参数的位置和参数的类型,来区分重载的构造器。
  1.  
    <!--
  2.  
    id:我们从IOC容器取出的唯一标识,一般是类名的首字母小写
  3.  
    class:bean的全类名,通过反射的方式来创建bean,所以要求bean必须有无参的构造器
  4.  
    constructor-arg:value是赋值,index是参数的位置,type是参数的类型
  5.  
    -->
  6.  
    <bean id="car2" class="com.dhx.Car">
  7.  
    <constructor-arg value="baoma" index="1"></constructor-arg>
  8.  
    <constructor-arg value="beijing" index="0"></constructor-arg>
  9.  
    <constructor-arg value="240" type="int"></constructor-arg>
  10.  
    </bean>

 

转载:https://zhuanlan.zhihu.com/p/90939765


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM