如何在线程中获取spring 管理的bean
转载自: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的更多相关文章
- Servlet中获取Spring管理的bean
描述: 在Servlet中调用Spring管理的接口,可以使Dao/Service/ServiceImpl. 前提是在调用的bean中有注解: @Repository("beanName&q ...
- 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 ...
- 170630、springboot编程之普通类中调用spring管理的bean对象
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...
- 在Servlet中获取Spring注解的bean
最近由于项目中出现了Servlet调用Spring的bean,由于整个项目中所有的bean均是注解方式完成,如@Service,@Repository,@Resource等,但是Spring的容器管理 ...
- 获取Spring管理的Bean
1.再Spring配置文件中配置工具类 <!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spr ...
- 如何在servlet中获取spring创建的bean
package com.yxf.controller; import java.io.IOException; import javax.servlet.ServletException; impor ...
- 怎么随时获取Spring的上下文ApplicaitonContext,和Spring管理的Bean
BeanFactory接口 Interface BeanFactory getBean <T> T getBean(String name, Class<T> required ...
随机推荐
- Linux下安装LAMP(Apache+PHP+MySql)和禅道
1.更新yum源: yum update -y 2.安装Apache+PHP+MySql yum install httpd mysql-devel mysql-server mysql-php ph ...
- OpenCV 学习笔记03 边界框、最小矩形区域和最小闭圆的轮廓
本节代码使用的opencv-python 4.0.1,numpy 1.15.4 + mkl 使用图片为 Mjolnir_Round_Car_Magnet_300x300.jpg 代码如下: impor ...
- win7 32位 安装opencv-python后,运行时提示 "from .cv2 import *: DLL load failed: 找不到指定的模块" 的解决办法
安装opencv后,运行一个测试程序提示"from .cv2 import *: DLL load failed: 找不到指定的模块".于是百度一下解决办法,结果试了N多方法后也没 ...
- android图片等比例缩放 填充屏幕
在ImageView的t同事设置两个属性 android:adjustViewBounds="true"android:scaleType="fitXY"
- Transparent Huge Pages
在RHEL6中,透明大页功能是默认开启的. 开启该选项后,内核会尽可能地尝试分配大页,如果mmap区域是2mb,那么每个linux进程都会分配到2mb大小的页.如果大页不够用了(比如物理内存不够了), ...
- 豆瓣上9分以上的IT书籍-编程技术篇
在豆瓣上9分以上的IT书籍-编程语言篇中,收集了很多优秀的编程语言书籍,也得到了不少读者的喜欢.不过也有一些读者留言说某某书为什么没有,一种是因为某些书并不算讲某种编程语言的,一种是由于豆瓣9分以上这 ...
- The password supplied with the username Domain\UserName was not correct. Verify that it was entered correctly and try again
起因 今天想进入SharePoint 2013 Central Administration创建一个WebApplication,尽然发生了错误: The password supplied with ...
- stm32定时器PWM模式和输出比较模式
pwm模式是输出比较模式的一种特例,包含于输出比较模式中 /** @defgroup TIM_Output_Compare_and_PWM_modes * @{ */ #define TIM_OCMo ...
- Android 获取自带浏览器上网记录
先是搜索了一下,在manifest里添加 <uses-permission android:name="com.android.browser.permission.READ_HIST ...
- Android帧布局<TabHost>标签
先贴上一段代码: main.xml: <p><?xml version="1.0" encoding="utf-8"?> <Tab ...