关于spring:一起来读官方文档SpringIOC03

1.4。依存关系

典型的企业应用程序不坑你只蕴含单个对象或单个bean。即便是最简略的应用程序,也有一些对象能够协同工作,以出现最终用户视为统一的应用程序。下一部分将阐明如何从定义多个独立的Bean定义到实现对象合作以实现目标的齐全实现的应用程序。

1.4.1。依赖注入

依赖注入(Dependency injection,DI)是一个过程,对象通过结构函数参数、工厂办法的参数或在结构或从工厂办法返回后在对象实例上设置的属性来定义它们的依赖项(即与它们一起工作的其余对象)。
而后,容器在创立bean时注入这些依赖项。
这个过程基本上是bean自身的反向(因而得名,管制的反转),通过应用类的间接结构或服务定位器模式,bean本人管制其依赖项的实例化或地位。

应用DI准则,代码会更清晰,并且当向对象提供它们的依赖时,解耦会更无效。对象不查找其依赖项,也不晓得依赖项的地位或类。因而,您的类变得更容易测试,特地是当依赖关系在接口或形象基类上时,它们容许在单元测试中应用stub或mock实现。

依赖注入有两种次要的变体:基于结构器的依赖注入和基于setter的依赖注入。
基于构造函数的依赖注入

基于构造函数的DI是通过容器调用具备多个参数(每个参数代表一个依赖项)的构造函数来实现的。调用static带有特定参数的工厂办法来结构Bean简直是等效的,并且本次探讨将构造函数和static工厂办法的参数视为相似。以下示例显示了只能通过构造函数注入进行依赖项注入的类:

public class SimpleMovieLister {

    // SimpleMovieLister 依赖项 MovieFinder
    private MovieFinder movieFinder;

    // 构造函数,以便Spring容器可能注入MovieFinder
    public SimpleMovieLister(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

}

留神,该类没有什么特地的。它是一个POJO,不依赖于特定于容器的接口,基类或正文。

结构函数参数解析

结构函数参数解析匹配通过应用参数的类型进行。如果Bean定义的结构函数参数中不存在潜在的歧义,则在实例化Bean时,在Bean定义中定义结构函数参数的程序就是将这些参数提供给适当的构造函数的程序。思考以下类别:

package x.y;
public class ThingOne {

    public ThingOne(ThingTwo thingTwo, ThingThree thingThree) {
        // ...
    }
}

假如ThingTwo和ThingThree类不是通过继承关联的,则不存在潜在的歧义。因而,以下配置能够失常工作,并且您无需在<constructor-arg/> 元素中显式指定结构函数参数索引或类型。

<beans>
    <bean id="beanOne" class="x.y.ThingOne">
        <constructor-arg ref="beanTwo"/>
        <constructor-arg ref="beanThree"/>
    </bean>
    <bean id="beanTwo" class="x.y.ThingTwo"/>
    <bean id="beanThree" class="x.y.ThingThree"/>
</beans>

当援用另一个bean时,类型是已知的,并且能够产生匹配(与后面的示例一样)。当应用简略的类型(例如)时 <value>true</value>,Spring无奈确定值的类型,因而在没有帮忙的状况下无奈按类型进行匹配。思考以下类别:

package examples;
public class ExampleBean {

    private int years;
    private String ultimateAnswer;

    public ExampleBean(int years, String ultimateAnswer) {
        this.years = years;
        this.ultimateAnswer = ultimateAnswer;
    }
}
结构函数参数类型匹配

在上述情况下,如果通过应用type属性显式指定结构函数参数的类型,则容器能够应用简略类型的类型匹配。如下例所示:

<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg type="int" value="7500000"/>
    <constructor-arg type="java.lang.String" value="42"/>
</bean>
结构函数参数索引

您能够应用该index属性来明确指定结构函数参数的索引,如以下示例所示:

<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg index="0" value="7500000"/>
    <constructor-arg index="1" value="42"/>
</bean>

除了解决多个简略值的歧义性之外,指定索引还能够解决歧义,其中构造函数具备两个雷同类型的参数。

索引从0开始。
结构函数参数名称

您还能够应用结构函数参数名称来打消歧义,如以下示例所示:

<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg name="years" value="7500000"/>
    <constructor-arg name="ultimateAnswer" value="42"/>
</bean>

请记住,要立刻应用该性能,必须在启用调试标记的状况下编译代码,
以便Spring能够从构造函数中查找参数名称。
如果您不能或不想应用debug标记编译代码,则能够应用 @ConstructorProperties JDK正文显式命名结构函数参数。
而后,该示例类必须如下所示:

package examples;
public class ExampleBean {

    @ConstructorProperties({"years", "ultimateAnswer"})
    public ExampleBean(int years, String ultimateAnswer) {
        this.years = years;
        this.ultimateAnswer = ultimateAnswer;
    }
}
基于Setter的依赖注入

基于设置器的DI是通过在调用无参数构造函数或无参数static工厂办法实例化bean 之后,在bean上调用setter办法来实现的。

上面的示例显示只能通过应用纯setter注入来依赖注入的类。此类是惯例的Java。它是一个POJO,不依赖于容器特定的接口,基类或正文。

public class SimpleMovieLister {
    private MovieFinder movieFinder;

    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
}
并不是写了上边的类并把这个bean注册进入Spring容器就能用setter注入
以下三种注册bean的形式 只有 autowire = byType, autowire=byName 才会应用setter注入 
autowire 默认属性就是 no,具体注入规定往后看
<bean id="simpleMovieLister" class="org.springframework.SimpleMovieLister" autowire="no"/>
<bean id="simpleMovieLister" class="org.springframework.SimpleMovieLister" autowire="byType"/>
<bean id="simpleMovieLister" class="org.springframework.SimpleMovieLister" autowire="byName"/>

ApplicationContext为它治理的bean反对基于结构器和基于setter的DI。在曾经通过构造函数办法注入了一些依赖项之后,它还反对基于setter的DI。您以BeanDefinition的模式配置依赖项,您将它与PropertyEditor实例一起应用,以将属性从一种格局转换为另一种格局。然而,大多数Spring用户并不间接应用这些类(即通过编程形式),而是应用XML bean定义、带正文的组件(即用@Component、@Controller等正文的类)或基于java的@Configuration类中的@Bean办法。而后在外部将这些源转换为BeanDefinition的实例,并用于加载整个Spring IoC容器实例。

###### 基于构造函数或基于setter的DI?
因为能够混合应用基于构造函数的DI和基于setter的DI,因而将构造函数用于强制性依赖项并将setter办法或配置办法用于可选依赖性是一个很好的教训法令。
留神,能够 在setter办法上应用@Required批注,以使该属性成为必须的依赖项。然而,最好应用带有参数的程序验证的构造函数注入。

Spring团队通常提倡构造函数注入,因为它能够让您将应用程序组件实现为不可变对象,并确保不存在必须的依赖项null。
此外,注入构造函数的组件始终以齐全初始化的状态返回到客户端(调用)代码。
附带阐明一下,大量的构造函数自变量是一种不好的代码味,这表明该类可能承当了太多的职责,应将其重构以更好地解决关注点拆散问题。

Setter注入次要应仅用于能够在类中调配正当的默认值的可选依赖项。
否则,必须在代码应用依赖项的任何中央执行非空查看。
setter注入的一个益处是,setter办法使该类的对象在当前能够重新配置或从新注入。
因而,通过JMX MBean进行治理是用于setter注入的引人注目的用例。

应用最适宜特定班级的DI格调。
有时,在解决您没有源代码的第三方类时,将为您做出抉择。
例如,如果第三方类未公开任何setter办法,则构造函数注入可能是DI的惟一可用模式。
依赖性解析过程

容器执行bean依赖项解析,如下所示:

  • 用形容所有bean的配置元数据创立和初始化ApplicationContext。配置元数据能够由XML、Java代码或正文指定。。
  • 对于每个bean,其依赖关系都以属性,结构函数参数或static-factory办法的参数的模式示意(如果应用它而不是一般的构造函数)。在理论创立Bean时,会将这些依赖项提供给Bean。
  • 每个属性或结构函数参数都是要设置的值的理论定义,或者是对容器中另一个bean的援用。
  • 作为值的每个属性或结构函数参数都将从其指定的格局转换为该属性或结构函数参数的理论类型。默认状况下,Spring可能以String类型提供值转换成所有内置类型,比方int, long,String,boolean等等。

在创立容器时,Spring容器验证每个bean的配置。
然而,bean属性自身在理论创立bean之前是不会设置的。
在创立容器时,将创立单例范畴并设置为预实例化(缺省值)的bean。
作用域在Bean作用域中定义。
否则,只在申请bean时创立它。创立一个bean可能会导致须要创立一系列的bean甚至是循环依赖的bean,因为须要创立和调配bean的依赖项和bean的依赖项的依赖项(等等)。
留神,这些依赖项之间的解析不匹配会在第一次创立受影响的bean时呈现。

###### 循环依赖
与典型的状况不同(没有循环依赖关系),bean A和bean B之间的循环依赖关系迫使一个bean在齐全初始化之前注入另一个bean(典型的先有鸡还是先有蛋的场景)。

如果您次要应用构造函数注入,那么就会创立一个无奈解决的循环依赖场景。

例如:类A通过构造函数注入须要一个类B的实例,类B通过构造函数注入须要一个类A的实例。如果您为类A和类B配置了互相注入的bean,那么Spring IoC容器在运行时检测到这个循环援用,并抛出BeanCurrentlyInCreationException。

一个可能的解决方案是编辑一些类的源代码,以便应用setter注入而不是结构器注入。或者,防止构造函数注入,只应用setter注入。换句话说,只管不倡议这样做,但您能够应用setter注入配置循环依赖关系。

您通常能够置信Spring会做正确的事件。
它在容器加载时检测bean不存在和循环依赖关系的问题。
Spring当bean理论创立时会尽可能晚地设置属性并解析依赖项。
这意味着,正确加载的Spring容器在创建对象或其依赖项时呈现问题时,能够在申请对象时生成异样——例如,bean因为短少或有效的属性而抛出异样。
某些配置问题的潜在提早可见性是ApplicationContext实现在默认状况下预实例化单例bean的起因。因为在理论须要之前创立这些bean须要破费一些后期工夫和内存,所以默认是在创立ApplicationContext时发现配置问题,而不是稍后发现。
您依然能够笼罩这个默认行为,以便单例bean能够惰性地初始化,而不是事后实例化。

如果不存在循环依赖关系,那么当一个或多个合作bean被注入到依赖bean中时,每个合作bean在被注入到依赖bean之前都已被齐全配置。
这意味着,如果bean A依赖bean B, Spring IoC容器会在调用bean A的setter办法之前齐全配置bean B。
换句话说,bean实例化(如果它不是一个单例事后实例化),其设置依赖项,相干的生命周期办法(如InitializingBean init办法或配置回调办法)调用。

依赖注入的例子

以下示例将基于XML的配置元数据用于基于setter的DI。Spring XML配置文件的一小部分指定了一些bean定义,如下所示:

此处examoleBean 的 autowire 默认属性就是 no,手动指定了 属性对应的bean
<bean id="exampleBean" class="examples.ExampleBean">
    <!-- setter injection using the nested ref element -->
    <property name="beanOne">
        <ref bean="anotherExampleBean"/>
    </property>

    <!-- setter injection using the neater ref attribute -->
    <property name="beanTwo" ref="yetAnotherBean"/>
    <property name="integerProperty" value="1"/>
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

public class ExampleBean {

    private AnotherBean beanOne;
    private YetAnotherBean beanTwo;
    private int i;

    public void setBeanOne(AnotherBean beanOne) {
        this.beanOne = beanOne;
    }

    public void setBeanTwo(YetAnotherBean beanTwo) {
        this.beanTwo = beanTwo;
    }
 
    public void setIntegerProperty(int i) {
        this.i = i;
    }
}

在后面的示例中,申明了setter以与XML文件中指定的属性匹配。以下示例应用基于构造函数的DI:

<bean id="exampleBean" class="examples.ExampleBean">
    <!-- constructor injection using the nested ref element -->
    <constructor-arg>
        <ref bean="anotherExampleBean"/>
    </constructor-arg>

    <!-- constructor injection using the neater ref attribute -->
    <constructor-arg ref="yetAnotherBean"/>

    <constructor-arg type="int" value="1"/>
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

public class ExampleBean {

    private AnotherBean beanOne;
    private YetAnotherBean beanTwo;
    private int i;

    public ExampleBean(
        AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
        this.beanOne = anotherBean;
        this.beanTwo = yetAnotherBean;
        this.i = i;
    }
}

Bean定义中指定的结构函数参数用作的构造函数的参数ExampleBean。
当初思考该示例的一个变体,在该变体中,不是应用构造函数,而是通知Spring调用static工厂办法以返回对象的实例:

<bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance">
    <constructor-arg ref="anotherExampleBean"/>
    <constructor-arg ref="yetAnotherBean"/>
    <constructor-arg value="1"/>
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

public class ExampleBean {

    private AnotherBean beanOne;
    private YetAnotherBean beanTwo;
    private int i;

    private ExampleBean(
        AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
        this.beanOne = anotherBean;
        this.beanTwo = yetAnotherBean;
        this.i = i;
    }

    public static ExampleBean createInstance (
        AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {

        ExampleBean eb = new ExampleBean (...);
        // some other operations...
        return eb;
    }
}

static工厂办法的参数由<constructor-arg/>元素提供,就像理论应用构造函数一样。factory办法返回的类的类型不用与蕴含staticfactory办法的类具备雷同的类型(只管在此示例中是)。实例(非动态)工厂办法能够以实质上雷同的形式应用(除了应用factory-bean属性代替应用class属性之外),因而在此不探讨这些细节。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理