<span style="font-size:10px;">package com.record.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import java.util.Map; /**
* 以静态变量保存Spring ApplicationContext.
*/
@SuppressWarnings("unchecked")
public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; public static ApplicationContext getApplicationContext() {
if (applicationContext == null)
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextUtil");
return applicationContext;
} /**
* ApplicationContextAware接口的context注入函数.
*/
public void setApplicationContext(ApplicationContext context) throws BeansException {
applicationContext = context;
} public static <T> T getBean(String name) throws BeansException {
return (T) applicationContext.getBean(name);
} public static <T> T getBean(Class<T> cls) {
return getBean(cls, applicationContext);
} public static <T> T getBean(Class<T> cls, ApplicationContext ctx) {
Map<String, T> map = ctx.getBeansOfType(cls);
if (map == null || map.size() == 0) {
return null;
}
if (map.size() > 1) {
new Exception("bean is not unique.").printStackTrace();
}
return map.values().iterator().next();
}
}</span>
<span style="font-size:10px;">//对于无法获取spring容器的class 如servlet等 可以方便的获取spring容器中的bean, 配合 servlet3.0注解(如@WebServlet(urlPatterns = "/mobile/report/billExcel", asyncSupported = true))等 对原有项目可以做到完全不侵入,爽歪歪啊</span>

获取spring bean的utils的更多相关文章

  1. SpringBoot 注册拦截器方式及拦截器如何获取spring bean实例

    SpringBoot 注册拦截器时,如果用New对象的方式的话,如下: private void addTokenForMallInterceptor(InterceptorRegistry regi ...

  2. filter中获取spring bean

    import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import ja ...

  3. [十]SpringBoot 之 普通类获取Spring容器中的bean

    我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...

  4. SpringBoot 之 普通类获取Spring容器中的bean

    [十]SpringBoot 之 普通类获取Spring容器中的bean   我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...

  5. 如何在静态方法或非Spring Bean中注入Spring Bean

           在项目中有时需要根据需要在自己new一个对象,或者在某些util方法或属性中获取Spring Bean对象,从而完成某些工作,但是由于自己new的对象和util方法并不是受Spring所 ...

  6. 配置springmvc在其他类中(spring容器外)获取注入bean

    学习https://github.com/thinkgem/jeesite 今天在写JedisUtils的时候要注入JedisPool,而这个属性被设置为static,@Resource和@Autow ...

  7. 【Spring】手动获取spring容器对象时,报no qualifying bean of type is defined

    手动获取容器对象时,报no qualifying bean of type is defined, 经过调查,发现手动获取的时候,该类所在的包必须经过spring容器初始化. 1.SpringConf ...

  8. Java代码获取spring 容器的bean几种方式

    一.目的 写了一个项目,多个module,然后想在A模块中实现固定的config注入,当B模块引用A时候,能够直接填写相对应的配置信息就行了.但是遇到一个问题,B引用A时候,A的配置信息总是填充不了, ...

  9. 单元测试之获取Spring下所有Bean

    单元测试中,针对接口的测试是必须的,但是如何非常方便的获取Spring注册的Bean呢? 如果可以获取所有的Bean,这样就可以将这个方法放到基类中,方便后面所有单元测试类的使用,具体实现如下: im ...

随机推荐

  1. TortoiseGit - 处理冲突

    处理冲突 冲突:远程的master已经被其他人更新到 2repo add 12,但是自己当前的工作区在未pull到最新前,增加了1repo add 12的改动. 右击最新的节点,选择Merge to ...

  2. 手动编译生成apk

    转载一篇介绍如何去手动编译生成apk的文章: http://jojol-zhou.iteye.com/blog/729254

  3. linux服务器的操作禁忌

    1.linux系统是否支持开启SELINUX服务 我方linux系统的服务器不支持开启Selinux服务,如果开启了selinux服务,会导致系统异常并无法启动. 2.linux系统下能否开启NetW ...

  4. ecshop php5.4以上版本错误之preg_replace 替换成 preg_replace_callback

    类似这样的报错: Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instea ...

  5. 查看Android下生成的.db数据库

    1.在cmd中找到sdk中的platform-tools文件夹. 2.输入adb shell命令. 3.再输入sqlite3 /data/data/com.svs.db/databases/svs.d ...

  6. php学习的第8天

    晚上老师布置啦17题算数问题的作业 我一看 感觉挺容易的 动手就开始做啦起来 不知不觉也9点多啊 终于做好前部题目 发现这我3个月前也遇到类似的题目 那是我还一题都做一题用上1个钟左右 好多也做不出 ...

  7. Raft协议详解-leader发送心跳代码go

    如果已经把最新的log更新了,那就多等一会,反之,很快就广播AppendEntries(也就是心跳消息) func (rf *Raft) LeaderState() { time.Sleep(10 * ...

  8. iOS打上线包或者测试包详细流程

    首先登陆苹果官方开发者账号:http://developer.apple.com 进入到如下界面 之后进入如下界面:点击第二项创建证书 整个流程如下图4步 然后按照如下图片进行配置即可 接下来去创建C ...

  9. win7系统,apache2.2下添加PHP5的配置详解

    首先要说apache(服务器). php(开发语言). mysql(数据库) 之间的关系. Apache:为系统提供了Web服务支持,网站:http://www.apache.org/ PHP:为系统 ...

  10. List------Linked 链表

    1.Definition Linked list consists of a series of nodes. Each nodes contains the element and a pointe ...