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/01 Report--
This article mainly explains "how to solve Spring @ Resource injection for null under multithreading", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Spring in multithreaded @ Resource injection for null how to solve it"!
Multithreaded @ Resource injection for null antecedent
Recording the following pit, my case goes something like this. I'm writing a crawler with webmagic, injecting an instance of the dao interface into the class through @ Resoource (same as @ Autowired).
Then I referenced this instance in the process method and found that it was always null here, and then I referenced the dao instance in the test class or in the main function of the current class, and everything was fine. )
After several twists and turns, it is found that the reason is spring and multi-thread safety problems, do not allow injection. Because I haven't studied the webmagic source code either, but I personally think it may have something to do with the fact that webmagic is not associated with the spring container.
Solution
I use a very simple and violent method, spring does not allow automatic injection, then manual injection, in the process method through ApplicationContext to get the corresponding dao bean instance, and then it is OK.
Other solutions are attached
It is I who declare a bean with @ service or @ resposity, which is managed by sping. Now I have a requirement, which is to use Thread, but this thread needs to use my service or dao, and then I habitually write this in a class that inherits the Thread class or implements Runnable:
@ Resourceprivate MyService myService
But at run time, when you enter this thread, the myServcie is always null, that is, the injection failed.
1. Checked, there is such a problem, the reason is spring and multi-thread safety problems, do not allow injection
The online solution said it was made into static when it was announced. As follows:
Private static CustomerchangeService customerchangeService; public CustomerchangeService getCustomerchangeService () {return customerchangeService;} public void setCustomerchangeService (CustomerchangeService customerchangeService) {CustomerchangeThread.customerchangeService = customerchangeService;}
But it won't work for me.
two。 The second kind (yes)
When I start this thread, I pass my service instance in the constructor of this thread, and then start it again.
New MyThread (myService) .start ()
3. The third kind (OK)
Just let my service or dao implementation class implement the interface Runnable, and then write the logic of your thread in the run method. When starting, just this.start (). Note that in addition to implementing the Runnable interface in the implementation class of service, you should also inherit the Runnable interface in the service interface.
In my case, I implemented Runnable directly in dao and then started it with the thread pool, as follows: pool.execute (this)
4. There is also an example of direct new.
I don't know if there is any good solution.
At this point, I believe you have a deeper understanding of "Spring in multithreaded @ Resource injection for null how to solve", might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.