不同作用域(scope)的Spring Bean之间的依赖关系的动态代理注入

Spring Bean可能具有不同的作用域(Scope),相同域的Spring Bean之间互相依赖基本没有问题。

但是不同域的Spring Bean之间互相依赖,如果不做特殊定义,则只能在实例化Spring Bean时注入其所依赖的其他Spring Bean。如果被注入的Spring Bean的作用域长,而注入其他Spring Bean的作用域短,似乎也没有问题,因为被注入的Spring Bean总是在作用域范围内。

但是,如果是要将作用域短的Spring Bean注入到作用域长的Spring Bean,则可能进行注入的时候,被注入的Spring Bean还无效。本文即主要解决该问题。


Spring框架中提供的解决方案就是在运行时注入实际被依赖的Spring Bean,而这只能通过动态代理实现。Spring框架提供了2种实现方式:
1.方法注入

利用Spring的应用上下文,初始化Spring Bean时无需注入任何Spring Bean(因为从XML的定义来看没有依赖任何其他Spring Bean)。但是在运行时,当调用到指定方法时,在Spring的应用上下文中查找指定的Spring Bean,并将匹配的Spring Bean实例化注入。示例,Spring框架的XML配置如下:

<!-- a stateful bean deployed as a prototype (non-singleton) -->
<bean id="command" class="fiona.apple.AsyncCommand" scope="prototype">
    <!-- inject dependencies here as required -->
</bean>

<!-- commandProcessor uses statefulCommandHelper -->
<bean id="commandManager" class="fiona.apple.CommandManager">
    <lookup-method name="createCommand" bean="command"/>
</bean>
说明:其中command是被依赖的Spring Bean,需要在CommandManager类的createCommand()方法被调用的时候注入command。


CommandManager类定义如下:

public abstract class CommandManager {

    public Object process(Object commandState) {
        // grab a new instance of the appropriate Command interface
        Command command = createCommand();
        // set the state on the (hopefully brand new) Command instance
        command.setState(commandState);
        return command.execute();
    }

    // okay... but where is the implementation of this method?
    protected abstract Command createCommand();
}

方法注入的实质是,利用CGLIB库的动态代理机制,在运行时通过ApplicationContext的getBean("commandManager",CommandManager.class)方法,创建CommandManager的子类并实例化,然后注入被依赖的Spring Bean。

为了降低耦合性,还可以在XML配置文件中利用<replaced-method ...>动态定义注入方法。


2.Spring AOP代理注入

初始化Spring Bean时注入的不是被依赖Bean(可能还不存在),而是一个代理对象。实际被调用时,再由代理对象调用实际被依赖的Bean。XML配置示例如下:

    <!-- an HTTP Session-scoped bean exposed as a proxy -->
    <bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
        <!-- instructs the container to proxy the surrounding bean -->
        <aop:scoped-proxy/>
    </bean>

    <!-- a singleton-scoped bean injected with a proxy to the above bean -->
    <bean id="userService" class="com.foo.SimpleUserService">
        <!-- a reference to the proxied userPreferences bean -->
        <property name="userPreferences" ref="userPreferences"/>
    </bean>
说明:userService依赖于userPreferences。初始化userService时,注入的是userPreferences的代理。运行时,代理会调用实际的userPreferences。

Spring AOP代理默认也是利用CGLIB库的动态代理机制。不过,还可以利用JDK本身提供的基于接口的代理机制(条件是被依赖的Spring Bean必须实现一个接口)。XML配置示例如下:

<!-- DefaultUserPreferences implements the UserPreferences interface -->
<bean id="userPreferences" class="com.foo.DefaultUserPreferences" scope="session">
    <aop:scoped-proxy proxy-target-class="false"/>
</bean>

<bean id="userManager" class="com.foo.UserManager">
    <property name="userPreferences" ref="userPreferences"/>
</bean>


你可能感兴趣的:(spring,动态代理,cglib,scope)