从spring容器中取出注入的bean 工具类,代码如下:

package com.hyzn.fw.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; /**
* @ClassName: SpringBeanUtil
* @Description: TODO Spring获取bean的工具类,可用于在线程里面获取bean
* 需要在 类上 标注 @Component ,否则没有将此工具类 注入到spring容器中
* @author xbq
* @version 1.0
* @date 2017-2-21 下午2:30:38
*/
@Component
public class SpringBeanUtil implements ApplicationContextAware{ private static ApplicationContext applicationContext = null; // 获取ApplicationContext对象
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringBeanUtil.applicationContext = applicationContext;
} /**
* @Title: getBeanByName
* @Description: TODO 通过bean的名字来获取Spring容器中的bean
* @param beanName
* @return
* @return: Object
*/
public static Object getBeanByName(String beanName) {
if (applicationContext == null){
return null;
}
return applicationContext.getBean(beanName);
} public static <T> T getBean(Class<T> type) {
return applicationContext.getBean(type);
}
}

最新文章

  1. bzoj4730: Alice和Bob又在玩游戏
  2. Deep Learning 5_深度学习UFLDL教程:PCA and Whitening_Exercise(斯坦福大学深度学习教程)
  3. Ext.Form 自动填写表单内容
  4. web机制简笔
  5. CSLA的项目结构(一)
  6. iOS-Storyboad动态刷新
  7. JQuery使用on绑定动态生成元素时碰到的问题
  8. 动态rem与1px边框问题的理解
  9. java学习——java按值传递和按址传递
  10. Python实现网站注册验证码生成类
  11. WordCount程序代码解
  12. Linux下文件的mtime/atime/ctime研究
  13. 轻松学习UML之类图,状态图
  14. LeetCode Smallest Range
  15. java 写一个 map reduce 矩阵相乘的案例
  16. IkAnalyzer2012FF_u1.jar免费下载
  17. fragment做成选项卡,tab效果。 fragment+RadioGroup
  18. 【BZOJ2395】【Balkan 2011】Timeismoney 最小乘积生成树
  19. MySQL Crash Course #09# Chapter 17. Combining Queries: UNION
  20. nginx的MainLine version、Stable version、Legacy versions

热门文章

  1. ssm项目开发通用base模块和工具
  2. 【Java】初始化过程
  3. mysql linux 区分大小写
  4. 用Python写一个猜数字游戏
  5. C#多线程解决界面卡死问题的完美解决方案,BeginInvoke而不是委托delegate 转载
  6. 常用shell命令实战
  7. 开源图形数据库Neo4j使用 php开发
  8. Altium Designer 小记
  9. r绘图基本
  10. ajax传值给php