Spring5参考指南:依赖注入(二)

简介: Spring5参考指南:依赖注入(二)

Null和Empty字符串值


Null和空字符串是不一样的,如下:


<bean class="ExampleBean">
    <property name="email" value=""/>
</bean>


上面的例子相当于:


exampleBean.setEmail("");


下面是怎么设置null:


<bean class="ExampleBean">
    <property name="email">
        <null/>
    </property>
</bean>


上面的例子相当于:


exampleBean.setEmail(null);


c-namespace


上面讲到了p-namespace专门是设置bean的属性用的,同样的c-namespace是用来设置构造函数参数的,如下所示:


<bean id="exampleBeanA" class="com.flydean.beans.ExampleBean">
        <constructor-arg type="int" value="7500000"/>
        <constructor-arg type="java.lang.String" value="42"/>
    </bean>
    <bean id="exampleBeanB" class="com.flydean.beans.ExampleBean"
    c:years ="10" c:ultimateAnswer="answer"
    />


depends-on


通常一个bean依赖另一个bean,我们会在XML中使用< ref/>来引用,但是这种依赖关系并不直接。我们可以使用depends-on属性来显式强制一个或多个bean在使用此元素的bean初始化之前进行初始化,如下所示:


<bean id="beanA" class="com.flydean.beans.SomeClass" depends-on="beanB"></bean>
    <bean id="beanB" class="com.flydean.beans.MovieFinder"></bean>


lazy-init


正常来说ApplicationContext中配置成单例模式的bean都会随Spring启动而初始化,如果有特殊的需要,想延长初始化该bean,则可以使用 lazy-init 。一个lazy-initialized bean告诉IOC容器在第一次请求bean实例时创建它,而不是在启动时。


<bean id="beanD" class="com.flydean.beans.MovieFinder" lazy-init="true"></bean>


但是,当一个惰性初始化bean是一个非惰性初始化的singleton bean的依赖项时

ApplicationContext会在启动时创建惰性初始化bean,因为它必须满足singleton的依赖项。


您还可以通过在< beans/>元素上使用默认的lazy init属性在容器级别控制lazy初始化,下面的示例显示:


<beans default-lazy-init="true">
    <!-- no beans will be pre-instantiated... -->
</beans>


自动装载


如果你想让Spring自动帮你注入bean的依赖bean时候,可以使用Spring的autowiring功能。autowiring 有4种模式:


模式 说明
no (默认)无自动装载。bean必须引用由ref定义的元素。对于较大的部署,不建议更改默认设置,因为显式指定合作者可以提供更大的控制度和清晰性。在某种程度上,它记录了系统的结构。
byName 按属性名称自动装载。Spring寻找与需要自动装载的属性同名的bean。例如,如果bean定义被设置为按名称自动装载,并且它包含一个master属性(即,它有一个setMaster(…)方法),那么spring将查找名为master的bean定义并使用它来设置该属性。
byType 如果容器中只有一个属性类型的bean,则允许自动装载属性。如果存在多个,则会引发一个致命的异常,这表示您不能为该bean使用byType自动装载。如果没有匹配的bean,则不会发生任何事情(未设置属性)。
constructor 类似于byType,但适用于构造函数参数。如果容器中不只有一个构造函数参数类型的bean,则会引发致命错误。


自动注入的限制和缺陷


虽然自动注入用起来很爽,但是它也有如下的缺陷:


  • property和constructor-arg的显示设置会覆盖自动注入,并且自动注入不能注入简单类型。


  • 自动注入不如显示配置精确。


  • 自动注入可能和容器中的很多bean相匹配。可能会出现问题。


从自动装载中排除Bean


使用autowire-candidate属性设置为false,可以防止bean被自动注入。该属性只会影响按类型注入的方式。如果按name注入,则不受影响。


下面是自动注入的例子:


<bean id="simpleMovieLister" class="com.flydean.beans.SimpleMovieLister" autowire="byType">
    </bean>
    <!--Setter DI -->
    <bean id="movieFinder" class="com.flydean.beans.MovieFinder">
        <property name="name" value="movie"/>
        <property name="number" value="123456"/>
    </bean>


SimpleMovieLister如下:


package com.flydean.beans;
import lombok.Data;
@Data
public class SimpleMovieLister {
    // the SimpleMovieLister has a dependency on the MovieFinder
    private MovieFinder movieFinder;
    // a setter method so that the Spring container can inject a MovieFinder
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
    // business logic that actually uses the injected MovieFinder is omitted...
}


在上面的例子中,movieFinder属性将会被自动注入。


方法注入


在bean的生命周期不同的时候,如果一个bean要依赖于另外一个bean则可能出现问题。 比如一个单例模式的beanA 依赖于多例模式的beanB。 因为beanA是单例模式的,所以在创建beanA的时候就已经将其依赖的beanB创建了,不可能在每次beanA需要beanB的时候都创建一个新的beanB的实例。


解决方法有很多种,我们一一进行讲解。


方法1:实现ApplicationContextAware


如果实现了ApplicationContextAware,则可以通过getBean方法在每次需要beanB的时候,请求他的新的实例,如下:


public class CommandManager implements ApplicationContextAware {
    private ApplicationContext applicationContext;
    public Object process(Map commandState) {
        // grab a new instance of the appropriate Command
        Command command = createCommand();
        // set the state on the (hopefully brand new) Command instance
        command.setState(commandState);
        return command.execute();
    }
    protected Command createCommand() {
        // notice the Spring API dependency!
        return this.applicationContext.getBean("command", Command.class);
    }
    public void setApplicationContext(
            ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}


这种方法并不可取的,因为业务代码和Spring框架产生了耦合。方法注入是Spring IoC 容器的一个高级特性,它可以很好的处理这种情况。


查找方法注入


查找方法注入是指容器重写container-managed bean上的方法,并返回容器中另一个命名bean。查找通常涉及一个原型bean,如前一节中描述的场景中所示。Spring框架通过使用cglib库中的字节码动态生成覆盖该方法的子类来实现该方法注入。


因为使用了cglib,所以bean不能是final类,方法也不能是final类型。


查找方法不适用于工厂方法,尤其不适用于配置类中的@Bean方法,因为在这种情况下,容器不负责创建实例,因此无法动态创建运行时生成的子类。


下面是一个查找方法的例子:


public abstract class CommandManagerB {
    public Object process(Map commandState) {
        // grab a new instance of the appropriate Command interface
        AsyncCommand command = createCommand();
        return null;
    }
    // okay... but where is the implementation of this method?
    public abstract AsyncCommand createCommand();
}


这里我们定义了一个抽象类,要查找的方法就是createCommand。返回的对象类,如下:


public class AsyncCommand {
}


下面是XML配置文件:


<!-- a stateful bean deployed as a prototype (non-singleton) -->
    <bean id="myCommand" class="com.flydean.beans.AsyncCommand" scope="prototype">
        <!-- inject dependencies here as required -->
    </bean>
    <!-- commandProcessor uses statefulCommandHelper -->
    <bean id="commandManager" class="com.flydean.beans.CommandManagerB">
        <lookup-method name="createCommand" bean="myCommand"/>
    </bean>


CommandMangerB每次调用createCommand,都会返回一个新的AsyncCommand实例。


在基于注解的情况下,也可以这样使用:


public abstract class CommandManagerC {
    public Object process(Object commandState) {
        Command command = createCommand();
        return command.execute();
    }
    @Lookup("myCommand")
    protected abstract Command createCommand();
}


其中@Lookup(“myCommand”) 中的名字也可以省略,则按照默认的类型来解析。

任意方法替换


我们甚至可以使用replaced-method替换bean的方法实现。我们有个MyValueCalculator类,有一个我们想重写的方法computeValue。


public class MyValueCalculator {
    public String computeValue(String input) {
        // some real code...
        return "abc";
    }
    // some other methods...
}


一个实现了org.springframework.beans.factory.support.MethodReplacer接口的类提供了新的方法,如下所示:


public class ReplacementComputeValue implements MethodReplacer {
    public Object reimplement(Object o, Method m, Object[] args) throws Throwable {
        // get the input value, work with it, and return a computed result
        String input = (String) args[0];
        return "def";
    }
}


bean的定义如下:


<bean id="myValueCalculator" class="com.flydean.beans.MyValueCalculator">
        <!-- arbitrary method replacement -->
        <replaced-method name="computeValue" replacer="replacementComputeValue">
            <arg-type>String</arg-type>
        </replaced-method>
    </bean>
    <bean id="replacementComputeValue" class="com.flydean.beans.ReplacementComputeValue"/>
相关文章
|
1月前
|
开发框架 Java Spring
Spring依赖注入以及使用建议
Spring依赖注入以及使用建议
30 0
|
2月前
|
XML Java 程序员
Spring的依赖注入
Spring的依赖注入
|
2月前
|
Java Spring
Spring5深入浅出篇:Spring中ioc(控制反转)与DI(依赖注入)
Spring5深入浅出篇:Spring中ioc(控制反转)与DI(依赖注入)
|
3月前
|
Java API Spring
Spring6-IoC(Inversion of Control)控制反转和DI(Dependency Injection)依赖注入,手动实现IOC
Spring6-IoC(Inversion of Control)控制反转和DI(Dependency Injection)依赖注入,手动实现IOC
|
1月前
|
Java 数据库连接 API
【Spring】1、Spring 框架的基本使用【读取配置文件、IoC、依赖注入的几种方式、FactoryBean】
【Spring】1、Spring 框架的基本使用【读取配置文件、IoC、依赖注入的几种方式、FactoryBean】
49 0
|
4月前
|
XML Java 数据格式
手写spring 框架——第二篇(依赖注入)
手写spring 框架——第二篇(依赖注入)
41 0
|
2月前
|
设计模式 开发框架 Java
Spring 依赖注入方式有哪些?
Spring 依赖注入方式有哪些?
84 0
Spring 依赖注入方式有哪些?
|
2月前
|
设计模式 Java 测试技术
Spring依赖注入的魔法:深入DI的实现原理【beans 五】
Spring依赖注入的魔法:深入DI的实现原理【beans 五】
127 0
|
2月前
|
安全 Java 开发者
Spring依赖注入大揭秘:@Autowired、@Qualifier和@Resource的区别与应用
Spring依赖注入大揭秘:@Autowired、@Qualifier和@Resource的区别与应用
60 0
|
4月前
|
XML Java 开发者
Spring框架: 什么是依赖注入(Dependency Injection)?
Spring框架: 什么是依赖注入(Dependency Injection)?
63 2