博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
不同作用域(scope)的Spring Bean之间的依赖关系的动态代理注入
阅读量:4179 次
发布时间:2019-05-26

本文共 1719 字,大约阅读时间需要 5 分钟。

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配置如下:

说明:其中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配置示例如下:

说明:userService依赖于userPreferences。初始化userService时,注入的是userPreferences的代理。运行时,代理会调用实际的userPreferences。

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

转载地址:http://pclai.baihongyu.com/

你可能感兴趣的文章
消灭编译警告(Warning)
查看>>
(GCC) How can I hide "defined but not used" warnings in GCC?
查看>>
错误: 隐式声明函数‘kmalloc’ [-Werror=implicit-function-declaration]
查看>>
error: two or more data types in declaration specifiers原因及解决方法
查看>>
Linux驱动基础开发2
查看>>
ioctl在socket中的一些用法及示例
查看>>
Linux设备驱动--块设备(二)之相关结构体
查看>>
Linux设备驱动--块设备(四)之“自造请求”
查看>>
Nand Flash和Nor Flash相关知识
查看>>
NAND flash和NOR flash的区别
查看>>
writeb(), writew(), writel(),readb(), readw(), readl() 宏函数
查看>>
NOR Flash擦写和原理分析
查看>>
51单片机程序执行流程(STARTUP.A51)
查看>>
原码, 反码, 补码 详解
查看>>
Java自学第一阶段(二)- 小试牛刀
查看>>
Java自学第一阶段(三)- 万能的变量
查看>>
Java自学第一阶段(四)-万能的变量(2)
查看>>
HashMap存储原理以及与hashcode、equals方法的关系
查看>>
python3.6在windows下安装scrapy遇到的问题总结
查看>>
pycharm中打开scrapy项目,import scrapy报错问题
查看>>