<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. java实现UDP聊天---转载

    import java.io.*; import java.net.*; class Send implements Runnable { private DatagramSocket ds; pub ...

  2. placeholder改变颜色

    ::-webkit-input-placeholder { /* WebKit browsers */ color: #cfcfcf; } :-moz-placeholder { /* Mozilla ...

  3. PHP7编译错误:php编译undefined reference to `libiconv 错误

    ext/gd/libgd/.libs/gdkanji.o: In function `do_convert’: /root/php-5.2.12/ext/gd/libgd/gdkanji.c:350: ...

  4. 转 s3c2440硬件学习----内存管理单元MMU

    本篇基本是韦东山书上的 一.内存管理单元MMU介绍 内存管理单元简称MMU,它负责虚拟地址到物理地址的映射,并提供硬件机制的内存访问权限检查.MMU使得每个用户进程拥有自己独立的地址空间,并通过内存访 ...

  5. Windows线程同步(上)

    先介绍一个创建线程的API,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms682453%28v=vs.85%29.aspx ...

  6. Windows API 之 FormatMessage

    FormatMessage Formats a message string. The function requires a message definition as input. The mes ...

  7. apache 不执行PHP,显示代码

    首先检查是否安装PHP,已经安装过的话,先执行 locate libphp5.so 查看APACHE是否有SO文件,如果没有,那就要重装PHP了,先执行php -i | grep configure ...

  8. HDU2629:Identity Card

    Problem Description Do you own an ID card?You must have a identity card number in your family's Hous ...

  9. 自动加载U盘

    编辑/etc/fstab 比如想在开机的时候将/dev/sda1安装在/mnt 可以在/etc/fstab中加入一行 /dev/sda1   /mnt    ext3    defaults    0 ...

  10. 把aspx页面输出成xml的方法注意事项

    先贴代码 Response.Charset = "gb2312"; Response.ContentType = "text/xml"; Response.Co ...