获取Spring管理的Bean
1.再Spring配置文件中配置工具类
<!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring bean对象 -->
<bean class="com.xxxxx.SpringContextHolder" lazy-init="false" />
2.编写java工具类
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
*
*/
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext; /**
* 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
*/
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationContext; // NOSONAR
} /**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
} /**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
checkApplicationContext();
return (T) applicationContext.getBean(name);
} /**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz) {
checkApplicationContext();
return (T) applicationContext.getBeansOfType(clazz);
} /**
* 清除applicationContext静态变量.
*/
public static void cleanApplicationContext() {
applicationContext = null;
} private static void checkApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
}
}
}
获取Spring管理的Bean的更多相关文章
- Servlet中获取Spring管理的bean
描述: 在Servlet中调用Spring管理的接口,可以使Dao/Service/ServiceImpl. 前提是在调用的bean中有注解: @Repository("beanName&q ...
- 如何在线程中获取spring 管理的bean
转载自:https://my.oschina.net/skyline520/blog/181158?fromerr=GjtR6Wec spring xml中定义 <!--spring 工具类-- ...
- tomcat启动后,在普通java类中获取spring管理的bean和ServletContext,(经过验证,可以取到)
//从spring容易中获取bean public static Object getBean(String beanName){ ApplicationContext context = Conte ...
- 在普通类中获取Spring管理的bean
1.在项目中添加下面的类: import org.springframework.context.ApplicationContext; import org.springframework.cont ...
- jsp中获取spring 管理的bean(通过config)
WebApplicationContext wac = (WebApplicationContext)config.getServletContext().getAttribute(WebApplic ...
- 怎么随时获取Spring的上下文ApplicaitonContext,和Spring管理的Bean
BeanFactory接口 Interface BeanFactory getBean <T> T getBean(String name, Class<T> required ...
- 为何在新线程中使用注解获取不到Spring管理的Bean
新建的线程类NewThread,在这个类中国使用Spring的注解获取Service,为null 网上已有这种问题的解决方案,但是为何在新线程中使用注解获取不到Spring管理的Bean? 问了老大, ...
- 手动获取被spring管理的bean对象工具
在netty handler开发中,我们无法将spring的依赖注入到Handler中,无法进行数据库的操作,这时候我们就需要手动获取被spring管理的bean对象: 创建一个 imp ...
- (转)Spring管理的Bean的生命周期
http://blog.csdn.net/yerenyuan_pku/article/details/52834011 bean的初始化时机 前面讲解了Spring容器管理的bean的作用域.接着我们 ...
随机推荐
- npm update常用命令使用
一.更新 npm-check检查更新 npm install -g npm-check npm-check 2. npm-upgrade更新 npm install -g npm-upgrade np ...
- 问题集录--JS如何处理和解析Json数据
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族 ...
- Lakeshore用户手册
1.场景 场景是游戏的基本组成部分,开始界面,结束界面,每个关卡都是一个场景.游戏中基于游戏的情节,可以在各个场景间跳转. 2.精灵 精灵可以理解为图片的容器.如果需要在游戏场景中插入一个静态图片,那 ...
- php数组操作,js数组操作
俩语言一块儿学老混.整理一下. php: 1.声明: 1)$arr1 = array('key1' => 'value1', 'key2' => 'value2' ...);//关联数组, ...
- SQL 除去回车符 除去空格符
update table set fa=replace(fa,chr(13),'') ; --- 除去回车符 update table set fa=replace(fa,' ','') ; --- ...
- sql 递归 STUFF
select distinct fm_id, ,,'') AS SO_Nums from [dbo].[t_BADItems] its 表内一对多 的关系查询
- 在 Azure 上创建和链接 Azure SQL 数据库
本快速入门介绍了如何在 Azure 门户中创建并连接 Azure SQL 数据库.在本教程中完成的所有操作均符合 1 元试用条件. 开始之前 如果您还没有 Azure 账户,可以申请 1 元试用账户. ...
- [PHP] Oauth授权和本地加密
1.Oauth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方 关键字:appKey appSecre ...
- jquery echarts 饼状图
var myChart = echarts.init(document.getElementById('myChart')); option = { title : { text: '某站点用户访问来 ...
- Filter内容
1.利用Filter来过滤的时候大都是Http请求和Http响应,在doFilter()方法中,参数类是ServletRequest和ServletResponse ,使用的时候一般需要强制转换为H ...