线程中获取SpringBean

阅读更多
1、Spring工具类
public class SpringApplicationContextHolder implements ApplicationContextAware {

	private static ApplicationContext context;

	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		SpringApplicationContextHolder.context = context;
	}

	public static Object getSpringBean(String beanName) {
		if (null == beanName || 0 == beanName.length()) {
			throw new IllegalArgumentException("beanName is required!");
		}
		return context == null ? null : context.getBean(beanName);
	}

	public static String[] getBeanDefinitionNames() {
		return context.getBeanDefinitionNames();
	}
}


2、spring.xml注册文件配置bean

	



3、线程中调用
IDailyReportService dailyReportService = (IDailyReportService) SpringApplicationContextHolder
					.getSpringBean("dailyReportService");
			
			String filePath = dailyReportService.createDailyReportExcel();

你可能感兴趣的:(spring,bean,thread)