Spring - 运行时获取bean(ApplicationContextAware接口)
默认情况下,我们的bean都是单例模式(即从容器初始化到销毁只保持一个实例)。当一个bean需要引用另外一个bean,我们往往会通过bean属性的方式通过依赖注入来引用另外一个bean。那么问题就来了,假设一个单例模式的bean A需要引用一个非单例模式的bean B,并且bean A的所有方法都用到了bean B,我们希望每次都拿到最新的bean B。如果用之前的依赖注入模式,bean A只能再实例化的时候通过setter方式或者构造函数方式拿到一次bean B,所以bean A不能在每次需要bean B的时候保证都拿到最新的bean B。
牛逼的Spring又给我们提供了解决方案,那就是ApplicationContextAware接口。通过ApplicationContextAware我们可以拿到容器(applicationContext对象),通过容器的getBean方法我们就可以拿到最新的bean B了。示例代码如下:
//a class that uses a stateful Command-style class to perform some processing
package fiona.apple; //Spring-API imports
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; 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耦合了,那么就背离了IoC原则(完全由容器管理bean)。
然后。。。spring又给我们提供了一个可以继续使用IoC又能实现上面场景的方法,那就是<lookup-method>标签。
Spring - 运行时获取bean(ApplicationContextAware接口)的更多相关文章
- 从Spring容器中获取Bean。ApplicationContextAware
引言:我们从几个方面有逻辑的讲述如何从Spring容器中获取Bean.(新手勿喷) 1.我们的目的是什么? 2.方法是什么(可变的细节)? 3.方法的原理是什么(不变的本质)? 1.我们的目的是什么? ...
- 对spring创建对象时为何要使用接口
对spring创建对象时为何要使用接口,而使用接口的实现类会报错 接上一篇问题的解答:Spring AOP获取不了增强类(额外方法)和无法通过getBean()获取对象 此问题发生在动态代理时,比如对 ...
- iOS开发——运行时OC篇&使用运行时获取系统的属性:使用自己的手势修改系统自带的手势
使用运行时获取系统的属性:使用自己的手势修改系统自带的手势 有的时候我需要实现一个功能,但是没有想到很好的方法或者想到了方法只是那个方法实现起来太麻烦,一或者确实为了装逼,我们就会想到iOS开发中最牛 ...
- swift3.0 运行时获取类的属性
//定义Person类 class Person: NSObject { var name: String? //注意这里基本数据类型我定义的是必选属性 var age: Int = override ...
- 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中的元素
[源码下载] 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中 ...
- spring根据beanName获取bean
spring根据beanName获取bean主要实现: org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean( ...
- FastJson序列化Json自定义返回字段,普通类从spring容器中获取bean
前言: 数据库的字段比如:price:1 ,返回需要price:1元. 这时两种途径修改: ① 比如sql中修改或者是在实体类转json前遍历修改. ②返回json,序列化时候修改.用到的是fastj ...
- 如何让Qt程序在运行时获取UAC权限
在pro文件中加入以下语句: QMAKE_LFLAGS += /MANIFESTUAC:\"level=\'requireAdministrator\' uiAccess=\'false\' ...
- iOS crash log 解析 symbol address = stack address - slide 运行时获取slide的api 利用dwarfdump从dsym文件中得到symbol
概述: 为什么 crash log 内 Exception Backtrace 部分的地址(stack address)不能从 dsym 文件中查出对应的代码? 因为 ASLR(Address spa ...
随机推荐
- Spring中Druid链接池的配置
本文记录了使用Druid的方法, 包括Spring和Spring boot中使用Druid的配置方法. Spring中配置Druid连接池 以链接mysql为例 1 添加druid依赖 <dep ...
- 使用Visual Studio Code和typescript 开发调试React Native项目
关于React Native的详细介绍我就不叙述了,他是使用js构建原声app的开发框架.一次变异多平台运行,非常强大.但是个人不喜欢js的过于灵活(弱类型)的语法.强大的强类型语言Typescrip ...
- Ubuntu 发行版的 Linux 操作系统
Ubuntu 发行版的 Linux 操作系统.. ------------------------------------- ------------------------------------- ...
- 《principles of model checking》中的离散时间马尔科夫链
<principles of model checking>中的离散时间马尔科夫链 说明:此文为我自学<principles of model checking>第十章内容的笔 ...
- 线性代数-矩阵-【4】点乘 C和C++的实现
点击这里可以跳转至 [1]矩阵汇总:http://www.cnblogs.com/HongYi-Liang/p/7287369.html [2]矩阵生成:http://www.cnblogs.com/ ...
- Ubuntu 环境 samba的安装&配置
一.samba的安装: sudo apt-get install samba sudo apt-get install smbfs sudo apt-get install cifs-utils (新 ...
- cocos2dx 在Xcode里面 resource 里面文件夹的搜索
以前一直在pc上写cocos2dx 第一次到Xcode里面写 发现在resource文件夹里面 添加了新的目录后竟然没有用 这里记录下 在项目里面和classes目录同级的 标签下有个Resourc ...
- 基于dijkstra算法求地铁站最短路径以及打印出所有的路径
拓展dijkstra算法,实现利用vector存储多条路径: #include <iostream> #include <vector> #include <stack& ...
- photoshop软件应用手记
------------------------常用图片格式------------------------ 位图和矢量图 位图也叫点阵图,是由一个个的方形的像素点排列在一起拼接而成的,位图在放大时, ...
- WebApi Ajax 跨域请求解决方法(CORS实现)(作者:jianxuanbing)
概述 ASP.NET Web API 的好用使用过的都知道,没有复杂的配置文件,一个简单的ApiController加上需要的Action就能工作.但是在使用API的时候总会遇到跨域请求的问题,特别各 ...