转载自:https://my.oschina.net/skyline520/blog/181158?fromerr=GjtR6Wec

spring xml中定义

<!--spring 工具类-->
<bean id="springContextUtil" class="com.skyline.pub.utils.SpringContextUtil"/>

SpringContextUtil的代码如下

package com.skyline.pub.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import java.util.Locale; /**
* Spring 获取 bean工具类
* Author: skyline{http://my.oschina.net/skyline520}
* Created: 13-6-12 上午7:44
*/
public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext context = null;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
} public static <T> T getBean(String beanName){
return (T) context.getBean(beanName);
} public static String getMessage(String key){
return context.getMessage(key, null, Locale.getDefault());
} }

然后在线程中直接使用 (注: uploadService 为spring 中配置的bean)

@Override
public void run() {
UploadService uploadService = SpringContextUtil.getBean("uploadService");
switch (sheetIndex){
case 1:uploadService.updateMiddleSaleProcedure(start,limit); break;
case 2:uploadService.updateProductCountProcedure();break;
case 3:uploadService.updateMonthProcedure();break;
}
countDownLatch.countDown();
}

如何在线程中获取spring 管理的bean的更多相关文章

  1. Servlet中获取Spring管理的bean

    描述: 在Servlet中调用Spring管理的接口,可以使Dao/Service/ServiceImpl. 前提是在调用的bean中有注解: @Repository("beanName&q ...

  2. tomcat启动后,在普通java类中获取spring管理的bean和ServletContext,(经过验证,可以取到)

    //从spring容易中获取bean public static Object getBean(String beanName){ ApplicationContext context = Conte ...

  3. 在普通类中获取Spring管理的bean

    1.在项目中添加下面的类: import org.springframework.context.ApplicationContext; import org.springframework.cont ...

  4. jsp中获取spring 管理的bean(通过config)

    WebApplicationContext wac = (WebApplicationContext)config.getServletContext().getAttribute(WebApplic ...

  5. 170630、springboot编程之普通类中调用spring管理的bean对象

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

  6. 在Servlet中获取Spring注解的bean

    最近由于项目中出现了Servlet调用Spring的bean,由于整个项目中所有的bean均是注解方式完成,如@Service,@Repository,@Resource等,但是Spring的容器管理 ...

  7. 获取Spring管理的Bean

    1.再Spring配置文件中配置工具类 <!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spr ...

  8. 如何在servlet中获取spring创建的bean

    package com.yxf.controller; import java.io.IOException; import javax.servlet.ServletException; impor ...

  9. 怎么随时获取Spring的上下文ApplicaitonContext,和Spring管理的Bean

    BeanFactory接口 Interface BeanFactory getBean <T> T getBean(String name, Class<T> required ...

随机推荐

  1. apk 静默安装

    老大要我弄个自动更新,要用到静默安装,网上找到了些大拿的代码,我拿去改吧改吧,先贴出来: /** * 软件静默安装 * @param apkAbsolutePath apk文件所在路径 * @retu ...

  2. 创建在“system.net/defaultProxy”配置节中指定的Web代理时出错解决办法。

    出现这种问题会有很多原因,大致解决方法 方法1:在CMD下输入netsh winsock reset命令 简单来说netsh winsock reset命令含义是重置 Winsock 目录.如果一台机 ...

  3. 恢复Ext3下被删除的文件

    下面是这个教程将教你如何在Ext3的文件系统中恢复被rm掉的文件. 假设我们有一个文件名叫 ‘test.txt’  $ls -il test.txt 15 -rw-rw-r– 2 root root ...

  4. 使用Openssl的AES加密算法

    原文链接: http://blog.csdn.net/yasi_xi/article/details/13997337 Openssl是很常见的C接口的库,个人觉得易用.以下是AES加密的使用备忘.如 ...

  5. Sublime Text增加Build system类型,打造一个全能IDE

    Sublime text2是一款非常方便的文本编辑器,现在我基本上不用IDE去编写代码,一般都是在Sublime text2中编辑,当然,这里无法执行.debug是软肋,于是上网找了下资料,可以把添加 ...

  6. Docker 建立镜像流程

    1.docker build from dockerfile http://www.runoob.com/docker/docker-build-command.html $ docker build ...

  7. 个人用户使用genymotion 模拟器

    genymotion android模拟器速度快,比较好用.对公司使用的是要收费的,但是对个人使用还是免费的,所以个人用户还可以继续使用.使用方法 1.注册账号,填写用户名.邮箱.密码.公司类型(选g ...

  8. IDEA快捷键收集

    生成set 和get方法 .生产重写方法Alt+Insert 查看类的所有方法alt + 7 去掉多余的引用包alt + ctrl + O ctrl + alt + t 生成try 语句 自动导入包 ...

  9. django http404 详解

    [引子] 今天在看django的官方文档的时候看到get_object_or_404这个函数感觉比较奇怪.这个主要来自于它的功能,如果要查询的对象 存在那么就返回对象:如果对象不存在那么就要报404 ...

  10. libsvm参数说明[zz]

    English:libsvm_options:-s svm_type : set type of SVM (default 0) 0 -- C-SVC 1 -- nu-SVC 2 -- one-cla ...