In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to solve the problem that SpringBoot multithreaded tasks can not be injected into bean by @ Autowired. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
When dealing with problems in multithreading, bean cannot be injected through @ Autowired. Null pointer exception is reported.
For thread safety, it is anti-injection, and if you want to use this class, you can only take an instance from the bean factory.
The solution is as follows:
1. Create a utility class code:
Package com.hqgd.pms.common;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component
/ * * @ Description: tool class for getting bean objects *
Author: yaorong * @ CreateDate: 2018-12-10 *
/ @ Componentpublic class SpringContextUtil implements ApplicationContextAware {
/ * * contextual object instance * / private static ApplicationContext applicationContext
@ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {SpringContextUtil.applicationContext = applicationContext;}
/ * get applicationContext *
* @ return *
/ public static ApplicationContext getApplicationContext () {
Return applicationContext;} / * *
* obtain Bean through name. * * @ param name * @ return * / public static Object getBean (String name) {return getApplicationContext () .getBean (name);} / * obtain Bean through class. *
* @ param clazz
* @ param * @ return * / public static T getBean (Class clazz) {return getApplicationContext () .getBean (clazz);} / * *
* return the specified Bean through name and Clazz *
* @ param name
* @ param clazz
* @ param * @ return
* / public static T getBean (String name, Class clazz) {
Return getApplicationContext () .getBean (name clazz)
}}
two。 Usage
@ Slf4j@Servicepublic class SerialPortService {
Public static SerialPort mSerialport = null
/ / private SimpMessagingTemplate simpMessage
Private DataAcquisitionService das; private SystemService systemService; private SysParamMapper sysParamMapper
Public SerialPortService () {
This.das = SpringContextUtil.getBean (DataAcquisitionService.class)
This.systemService = SpringContextUtil.getBean (SystemService.class)
This.sysParamMapper = SpringContextUtil.getBean (SysParamMapper.class)
}}
The above is about all the details about how to solve the problem that the SpringBoot multithreading task cannot be injected into bean by @ Autowired, and comprehensively introduces the programming technology, operating system, database, web front-end technology and so on.
After reading the above, do you have any further understanding of how to solve the problem that SpringBoot multithreaded tasks cannot be injected into bean by @ Autowired? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.