IOC中对象的创建、获取

目录

IOC创建对象?

BEAN有两种构造方法 ---- 有参构造 + 无参构造。

获取IOC创建的对象?

IOC与依赖注入?(DI)

常规依赖注入的三种方式?


IOC创建对象?

在Spring中,是在 spring.xml 中配置 bean 标签,IoC 容器通过加载 bean 标签来创建对象的。

BEAN有两种构造方法 ---- 有参构造 + 无参构造。

无参构造

<bean id=”stu” class=”xyz.hahatomato.entity.Student”</bean>

有参构造

<bean id=”stu2″ class=”xyz.hahatomato.entity.Student”>

<constructor-arg name=”id” value=”2″></constructor-arg>

<constructor-arg name=”name” value=”番茄”></constructor-arg>

<constructor-arg name=”age” value=”17″></constructor-arg>

</bean>

获取IOC创建的对象?

Spring提供了两种方法来获取这个对象:通过ID和运行时类

通过ID

1.加载 spring.xml 配置文件

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“spring.xml”);

2.通过 id 值获取对象

Student stu = (Student) applicationContext.getBean(“stu”);

通过运行时类

1.加载 spring.xml 配置文件

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“spring.xml”);

2.通过运行时类获取对象

Student stu = applicationContext.getBean(Student.class);

System.out.println(stu);

IOC与依赖注入?(DI)

依赖注入是将不同对象进行关联的一种方式。

例如创建了一个class对象后,再创建一个student对象,student对象中有一个属性是class,需要绑定刚刚创建的class。此时就需要用到依赖注入。

本质:

  • 依赖注入(Dependency Injection)和控制反转(Inversion of Control)是同一个概念。具体含义是:当某个角色(可能是一个Java实例,调用者)需要另一个角色(另一个Java实例,被调用者)的协助时,在传统的程序设计过程中,通常由调用者来创建被调用者的实例。但在Spring里,创建被调用者的工作不再由调用者来完成,因此称为控制反转。创建被调用者实例的工作通常由Spring容器来完成,然后注入调用者,因此也称为依赖注入。
  • IoC 是设计思想,DI 是具体的实现方式;
  • IoC 是理论,DI 是实践;

以下为xml配置方式举例:

<!– 创建 classes 对象 –>

<bean id=”classe1″ class=”xyz.hahatomato.entity.Classes”>

<property name=”id” value=”1″></property>

<property name=”name” value=”小班”></property>

</bean>

<!– 创建 stu 对象 –>

<bean id=”stu2″ class=”xyz.hahatomato.entity.Student”>

<property name=”id” value=”2″></property>

<property name=”name” value=”fish”</property>

<property name=”age” value=”3″></property>

<!– 将 classes 对象赋给 stu 对象 –>

<property name=”classe” ref=”classe1″></property>

</bean>

常规依赖注入的三种方式?

  1. 构造函数注入;
  2. 属性注入(setter注入);
  3. 接口注入;

其中具体原理如下:

  • 构造函数注入

直接在目的类(需要添加依赖的类)的构造函数中传入需要注入的对象,形成一个目的类的有参构造。

举例如下:

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();
    }
}

作者:uncle-lv
链接:https://zhuanlan.zhihu.com/p/90939765
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

  • 属性注入

在目的类的申明中,设置依赖类的属性,增加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);// 属性、setter注入
        stupidStudent.doHomework();
    }
}

 

  • 接口注入

在目的类的申明中,只在具体的功能函数中传入依赖类的实例。

举例如下:

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); // 只在特定功能函数中传入临时依赖类
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值