浅谈 Spring之 IOC (一)

274 阅读4分钟

本人刚毕业两年半,打算学习一些spring源码,提高自己的深度,前段时间一个机会知道了掘金这个平台,平时也有逛掘金,觉得这个平台给我们这些开发者一个很好的学习平台,本人也学习在上面学到了很多东西,所以,我也打算把自己所学的进行分享,如果写的有不对的地方,请指教。

想要深入了解IOC,点击当前链接看官方文档

IOC是什么

IOC 是控制反转的意思,官网是这样解释的:控制反转(IoC)原理的Spring框架实现。IoC也称为依赖注入(DI)。它是一个过程,对象仅通过构造函数参数、工厂方法的参数或在对象实例被构造或从工厂方法返回后在其上设置的属性来定义它们的依赖项(即它们使用的其他对象)。然后,容器在创建bean时注入这些依赖项。通俗的讲就是把对象的创建和控制权交给spring容器进行管理

IOC和DI有什么不同

官方说法是,IOC称为依赖注入(DI),个人感觉不太准确,其实是依赖注入(DI)是IOC的一种实现方式,IOC其实有两种实现方式:依赖查找,依赖注入。当面试的时候,面试官问你的时候,你可以这样回答: 官方的说IOC称为依赖注入(DI),个人感觉不太准确,其实是依赖注入(DI)是IOC的一种实现方式,IOC其实有两种实现方式:依赖查找,依赖注入。这样面试官肯定会另眼相看你的。因为你有自己的见解。

IOC是怎么创建对象的呢

怎么创建对象的呢?我相信大家都有答案了,对,底层就是通过反射去创建对象的。下面我们来说一下创建对象的几种方式。 可以参照官网bean的创建方式,大致就说了四种:

1、Instantiation with a Constructor(用构造函数实例化,这是我们最常用的) 构造方法也分为两种,有参构造和无参构造 (1)、无参构造

public class ExampleBean {
	public ExampleBean() {
    }
} 

public class ExampleBeanTwo {
    public ExampleBeanTwo(){
    }
}
<bean id="exampleBean" class="examples.ExampleBean"/>

<bean name="anotherExample" class="examples.ExampleBeanTwo"/>

(2)、有参构造

public class ExampleBean {
    private String name;
    
	public ExampleBean(String name) {
    	this.name = name
    }
    
} 

<!--第一种:下标赋值-->
<!--<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg index="0" value="peach"></constructor-arg>
</bean>-->

<!--第二种:类型赋值。
    如果两个参数都是String类型,则没有办法使用。
    所以不建议使用第二种
 -->
<!--<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg type="java.lang.String" value="peach"/>
</bean>-->

<!--第三种:直接通过参数名实现-->
<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg name="name" value="peach"></constructor-arg>
</bean>

2、Instantiation with a Static Factory Method(用静态工厂方法实例化)

<bean id="clientService"
    class="examples.ClientService"
    factory-method="createInstance"/>
public class ClientService {
    private static ClientService clientService = new ClientService();
    private ClientService() {}

    public static ClientService createInstance() {
        return clientService;
    }
}

3、Instantiation by Using an Instance Factory Method(通过使用实例工厂方法进行实例化)

<!-- the factory bean, which contains a method called createInstance() -->
<bean id="serviceLocator" class="examples.DefaultServiceLocator">
    <!-- inject any dependencies required by this locator bean -->
</bean>

<!-- the bean to be created via the factory bean -->
<bean id="clientService"
    factory-bean="serviceLocator"
    factory-method="createClientServiceInstance"/>
<!-- the factory bean, which contains a method called createInstance() -->
<bean id="serviceLocator" class="examples.DefaultServiceLocator">
    <!-- inject any dependencies required by this locator bean -->
</bean>

<!-- the bean to be created via the factory bean -->
<bean id="clientService"
    factory-bean="serviceLocator"
    factory-method="createClientServiceInstance"/>

4、Determining a Bean’s Runtime Type(确定Bean的运行时类型,没接触过,也不了解,这里就不深究了,知道有这种就行)

IOC创建的对象的作用域(Scope)有哪几种呢!

这里可以具体的看官方文档,了解最原生的东西,不含任何杂质

简单来说有六种,但是我们常用的就前面两种,好好学会前面两种就好,后面的四种做个了解,开发基本不用。不过六种我在这边给列出来

1、singleton:单例是对象默认bean定义的范围,也就是是说,如果你设置了对象的Scope是singleton,在你创建完对象后,你在每一个地方使用的这个类型的对象都是同一个对象,也就是说IOC容器中始终指挥有一个共享的实例对象。

<bean id="accountService" class="com.something.DefaultAccountService"/>

<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>

2、prototype: 当你把对象的Scope设置为prototype的时候,你每次请求创建对象的时候,都会创建不一样的对象,也就是创建的对象地址都不是同一个。

<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>

3、request:将单个bean定义定位到单个HTTP请求的生命周期。也就是说,每个HTTP请求都有它自己的bean实例,该实例是在单个bean定义的背面创建的。仅在支持web的Spring ApplicationContext上下文中有效。

4、session:将单个bean定义作用于HTTP会话的生命周期。仅在支持web的Spring ApplicationContext上下文中有效。

5、application:将单个bean定义作用于ServletContext的生命周期。仅在支持web的Spring ApplicationContext上下文中有效。

6、websocket:将单个bean定义作用于WebSocket的生命周期。仅在支持web的Spring ApplicationContext上下文中有效。

今天就先写到这吧,后期我会慢慢的推送新的ioc,尽量把自己的见解更多的分享出来。