In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how SpringBoot gets @ Service Bean classes in threads". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how SpringBoot gets @ Service Bean classes in threads".
How to get the @ Service Bean class in a thread
This is suitable for Springboot projects without Spring configuration files, where it is much more convenient to get bean if you have a configuration file.
The following figure shows a Mybatis Mapper Bean that I declare with the @ Service annotation, which is usually used by the class under the Springboot scan configuration.
@ Autowired annotation dependency injection.
I need to use it in a thread now, but Springboot naturally can only scan its own things, the Runnable interface implemented by the thread.
We're starting to solve the problem now.
1. First, create a configuration class that inherits ApplicationContextAware to get ApplicationContext.
Use the getBean method inside to get the Bean class you want.
This way you can get the Bean class you want in the thread. It's quite a pit!
Get bean object in multithreading
Note: in multithreaded scenarios, bean objects cannot be obtained by using the default spring autoassembly, and the bean can be obtained directly from the context context.
Create a class
Implement the ApplicationContextAware interface
Package com.bond.match.utils;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component;/** * Created with IntelliJ IDEA. * Date: 2018-1-11 0011 * Time: 13:20 * To change this template use File | Settings | File Templates. * / @ Componentpublic class ApplicationContextProvider implements ApplicationContextAware {private static ApplicationContext context; private ApplicationContextProvider () {} @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {context = applicationContext;} public static T getBean (Class aClass) {return context.getBean (aClass);}} how to call in multithreading
.method () is the method name of the bean object
ApplicationContextProvider.getBean (AccountAssetService.class). Method () above is all the content of the article "how SpringBoot gets the @ Service Bean class in the thread". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.