Springboot实现多线程注入bean的工具类操作
场景:使用springboot多线程,线程类无法自动注入需要的bean
解决方法:通过工具类获取需要的bean
工具类代码:
importorg.springframework.beans.BeansException; importorg.springframework.context.ApplicationContext; importorg.springframework.context.ApplicationContextAware; importorg.springframework.stereotype.Component; /** *@Description:获取bean对象的工具类 *@Author:ZhangLin *@CreateDate:2018/12/10 */ @Component publicclassApplicationContextProviderimplementsApplicationContextAware{ /** *上下文对象实例 */ privatestaticApplicationContextapplicationContext; @Override publicvoidsetApplicationContext(ApplicationContextapplicationContext)throwsBeansException{ this.applicationContext=applicationContext; } /** *获取applicationContext * *@return */ publicstaticApplicationContextgetApplicationContext(){ returnapplicationContext; } /** *通过name获取Bean. * *@paramname *@return */ publicstaticObjectgetBean(Stringname){ returngetApplicationContext().getBean(name); } /** *通过class获取Bean. * *@paramclazz *@param*@return */ publicstatic TgetBean(Class clazz){ returngetApplicationContext().getBean(clazz); } /** *通过name,以及Clazz返回指定的Bean * *@paramname *@paramclazz *@param *@return */ publicstatic TgetBean(Stringname,Class clazz){ returngetApplicationContext().getBean(name,clazz); } }
使用方法:
在线程类的构造函数里调用工具类的getBeans方法获取实例,如:
publicclassThreadAimplementsRunnable{ privateServiceservice; publicThreadA(){ this.service=ApplicationContextProvider.getBean(Service.class); } @Override publicvoidrun(){ //TOBEDONE } }
补充知识:在springboot中普通的线程类访问service类
1、首先在线程类上注解@Component
2、@Autowired
privateIStudentServicestudentService;
3、调用时候
studentService=SpringUtils.getBean("studentService");
4、SpringUtils
packagecom.ruoyi.common.utils.spring; importorg.springframework.beans.BeansException; importorg.springframework.beans.factory.NoSuchBeanDefinitionException; importorg.springframework.beans.factory.config.BeanFactoryPostProcessor; importorg.springframework.beans.factory.config.ConfigurableListableBeanFactory; importorg.springframework.context.ApplicationContext; importorg.springframework.context.ApplicationContextAware; importorg.springframework.stereotype.Component; /** *spring工具类方便在非spring管理环境中获取bean * *@authorruoyi */ @Component publicfinalclassSpringUtilsimplementsBeanFactoryPostProcessor,ApplicationContextAware { /**Spring应用上下文环境*/ privatestaticConfigurableListableBeanFactorybeanFactory; privatestaticApplicationContextapplicationContext=null; @Override publicvoidpostProcessBeanFactory(ConfigurableListableBeanFactorybeanFactory)throwsBeansException { SpringUtils.beanFactory=beanFactory; } /** *获取对象 * *@paramname *@returnObject一个以所给名字注册的bean的实例 *@throwsorg.springframework.beans.BeansException * */ @SuppressWarnings("unchecked") publicstaticTgetBean(Stringname)throwsBeansException { return(T)beanFactory.getBean(name); } /** *获取类型为requiredType的对象 * *@paramclz *@return *@throwsorg.springframework.beans.BeansException * */ publicstatic TgetBean(Class clz)throwsBeansException { Tresult=(T)beanFactory.getBean(clz); returnresult; } /** *如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true * *@paramname *@returnboolean */ publicstaticbooleancontainsBean(Stringname) { returnbeanFactory.containsBean(name); } /** *判断以给定名字注册的bean定义是一个singleton还是一个prototype。如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException) * *@paramname *@returnboolean *@throwsorg.springframework.beans.factory.NoSuchBeanDefinitionException * */ publicstaticbooleanisSingleton(Stringname)throwsNoSuchBeanDefinitionException { returnbeanFactory.isSingleton(name); } /** *@paramname *@returnClass注册对象的类型 *@throwsorg.springframework.beans.factory.NoSuchBeanDefinitionException * */ publicstaticClass>getType(Stringname)throwsNoSuchBeanDefinitionException { returnbeanFactory.getType(name); } /** *如果给定的bean名字在bean定义中有别名,则返回这些别名 * *@paramname *@return *@throwsorg.springframework.beans.factory.NoSuchBeanDefinitionException * */ publicstaticString[]getAliases(Stringname)throwsNoSuchBeanDefinitionException { returnbeanFactory.getAliases(name); } @Override publicvoidsetApplicationContext(ApplicationContextapplicationContext)throwsBeansException{ if(SpringUtils.applicationContext==null){ SpringUtils.applicationContext=applicationContext; } } //获取applicationContext publicstaticApplicationContextgetApplicationContext(){ returnapplicationContext; } }
以上这篇Springboot实现多线程注入bean的工具类操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。