如何在线程中获取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 ...
随机推荐
- Maven最佳实践:版本管理【转】
什么是版本管理 首先,这里说的版本管理(version management)不是指版本控制(version control),但是本文假设你拥有基本的版本控制的知识,了解subversion的基本用 ...
- 恢复oracle中用PLSQL误删除drop掉的表
一.查看回收站中表 select object_name,original_name,partition_name,type,ts_name,createtime,droptime from recy ...
- Unable to convert MySQL date/time value to System.DateTime
当使用.NET connector 连接MYSQL数据库,检索某些数据的时候,你可能会得到一个错误信息: "Unable to convert MySQL date/time value t ...
- 基于key/value+Hadoop HDFS 设计的存储系统的shell命令接口
对于hadoop HDFS 中的全部命令进行解析(当中操作流程是自己的想法有不允许见欢迎大家指正) 接口名称 功能 操作流程 get 将文件拷贝到本地文件系统 . 假设指定了多个源文件,本地目的端必须 ...
- 构建基于阿里云OSS文件上传服务
转载请注明来源:http://blog.csdn.net/loongshawn/article/details/50710132 <构建基于阿里云OSS文件上传服务> <构建基于OS ...
- spark.mllib源代码阅读-优化算法1-Gradient
Spark中定义的损失函数及梯度,在看源代码之前,先回想一下机器学习中定义了哪些损失函数,毕竟梯度求解是为优化求解损失函数服务的. 监督学习问题是在如果空间F中选取模型f作为决策函数.对于给定的输入X ...
- SharePoint 2013 Troubleshooting——启用 Developer Dashboard
SharePoint 2010的管理员和开发者可能对SharePoint Developer Dashboard(开发人员仪表盘)很熟悉.在SharePoint 2013这个工具已经被大范围的改写了, ...
- python pandas replace函数
在处理数据的时候,很多时候会遇到批量替换的情况,如果一个一个去修改效率过低,也容易出错.replace()是很好的方法. 1.基本结构: df.replace(to_replace, value) 前 ...
- [转]IC行业的牛人
转载的: 说来惭愧,我所了解的牛人也只是大学教授,工业界的高手了解的还太少,虽然我对教育界的牛人了解的也不多,但这里也要牢骚几句,论坛上的人好像只是认识Gray,Razavi,Allen,Lee, ...
- Java 利用 poi 生成 Excel文件的通用例子
在用java 写数据库应用的时候, 通常会生成各种报表,而这些报表可能会被导出为各种格式的文件,比如Excel文档,pdf 文档等等. 今天先做了一个生成Excel 文档的例子,主要解决以下问题: 生 ...