Solve the problem that @ Autowired fails to inject other objects into callable objects


Failed to inject other objects with @ Autowired in the callable object

The scenario is as follows:

I need to write a multithread to get the return value in an implementation class A, so I use callable. In addition to this implementation class A, I also write an implementation class B that specifically implements callable. In B, I inject another implementation class C with spring annotation @ Autowired. When the code runs, it is found that C can not be injected into B. my head hurts.

Current solution:

Write @ Autowired into class A, and put the B object directly into a as the internal class. In this way, it is easy to use C, but it is not clear why it is impossible to use @ Autowired or other methods to inject objects in B alone!

Spring @ Autowired annotation does not take effect


Default does not take effect. To take effect, you need to configure in XML: < context: annotation config >

Annotation 1 < context: component scan base package = “” > start package scanning

Annotation 2 < context: annotation config > enable injection annotation scanning

In general, two annotations are not used together

When “annotation 1” scans classes containing annotation @ component / @ service, the injected annotation takes effect automatically, and annotation 2 does not need to be configured

When the class to be scanned does not contain @ component / @ service and other annotations, and the injected annotation does not take effect, you only need to add annotation 2

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer.

Recommended Today

Explanation of websocket heartbeat reconnection

Recently, websocket has been used in the development of applet. The applet provides corresponding native API, which is different from H5 API. Therefore, some mature class libraries of popular H5 are difficult to use, and the native API has some defects, so it implements a set of heartbeat reconnection mechanism by itself. First, let’s briefly […]