DI依赖注入(Dependency Injection)

简介

依赖注入主要包括两个部分:

  • 依赖:对象的创建依赖于容器
  • 注入:对象的属性依赖于容器的注入

构造器注入

请查看Spring创建对象的方式。

set注入

<?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">
    <bean id="address" class="com.spring.pojo.Address">
        <property name="province" value="河南省"></property>
        <property name="city" value="郑州"></property>
    </bean>
    <bean id="student" class="com.spring.pojo.Student">
<!--        字符串-->
        <property name="name" value="张三"></property>
<!--        数字-->
        <property name="age" value="22"></property>
<!--        对象-->
        <property name="address" ref="address"></property>
<!--        数组-->
        <property name="cards">
            <array>
                <value type="java.lang.String">三好学生</value>
                <value type="java.lang.String">优秀班干部</value>
            </array>
        </property>
<!--        set-->
        <property name="friends">
            <set>
                <value type="java.lang.String">李四</value>
                <value>王五</value>
            </set>
        </property>
<!--        list-->
        <property name="plans">
            <list>
                <value>学习</value>
                <value>健身</value>
                <value>吃饭</value>
            </list>
        </property>
<!--        空值-->
        <property name="wife">
            <null/>
        </property>
<!--        Properties-->
        <property name="properties">
            <props>
                <prop key="父亲">老张</prop>
                <prop key="哥哥">大张</prop>
                <prop key="弟弟">张宝儿</prop>
            </props>
        </property>
<!--        boolean-->
        <property name="bad">
            <value>true</value>
        </property>
<!--        map-->
        <property name="scores">
            <map>
                <entry key="Chinese" value="140"></entry>
                <entry key="Math" value="140"></entry>
                <entry key="English" value="140"></entry>
            </map>
        </property>
    </bean>
</beans>

其它方式注入

命名空间的方式简化了需求:

  • p命名空间: 也就是set方式的注入
  • c命名空间: 也就是构造器方式的注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="address" class="com.spring.pojo.Address">
        <property name="province" value="河南省"></property>
        <property name="city" value="郑州"></property>
    </bean>

    <bean id="address" class="com.spring.pojo.Address" p:province="河南省" p:city="郑州"></bean>
    <bean id="address" class="com.spring.pojo.Address" c:province="河南省" c:city="郑州"></bean>
<beans/>   

bean的作用域

scope标签属性:

  • singleton单例模式

    <bean id="address" scope="singleton" class="com.spring.pojo.Address"></bean>

    每次取到的bean都是同一个对象。

  • prototype原型模式

    <bean id="address" scope="prototype" class="com.spring.pojo.Address"></bean>

    每次取到不同的对象。

    bean的生命周期

  • Spring 容器根据配置中的 bean 定义中实例化 bean。
  • Spring 使用依赖注入填充所有属性,如 bean 中所定义的配置。
  • 如果 bean 实现BeanNameAware 接口,则工厂通过传递 bean 的 ID 来调用setBeanName()。
  • 如果 bean 实现 BeanFactoryAware 接口,工厂通过传递自身的实例来调用 setBeanFactory()。
  • 如果存在与 bean 关联的任何BeanPostProcessors,则调用 preProcessBeforeInitialization() 方法。
  • 如果为 bean 指定了 init 方法(<bean>的 init-method) 属性那么将调用它。
  • 最后,如果存在与 bean 关联的任何 BeanPostProcessors,则将调用 postProcessAfterInitialization() 方法。
  • 如果 bean 实现DisposableBean 接口,当 spring 容器关闭时,会调用 destory()。
  • 如果为bean 指定了 destroy 方法( <bean> 的 destroy-method 属性),那么将调用它。

黑昆虫
1 声望0 粉丝

java 基础扎实,熟悉面向对象开发,了解 java 锁,juc,多线程等。


引用和评论

1 篇内容引用
0 条评论