Solution to @ Autowired dependency injection failure caused by spring AOP
Problems found:
Previously, I used springaop to make an operation log record. This time, when I used it on other classes, the service injection failed all the time. I found a lot of online content and found that everyone had similar situations, but they were not in line with their own situations. Later, I summarized my own situation and found that the method is private modified, which will lead to service injection failure during AOP adaptation, and the same service does not have this situation in other public methods, which is very strange.
Resolution process:
It is analyzed in combination with the available information: at org springframework. aop. support. In aoputils:
Here, method [] methods = clazz getmethods(); Only public methods can be obtained.
execution(* *(..)) The public / protected method can be matched, because if there is a match in the public, the target class will be proxied, and the pointcut matching can also be matched. Moreover, the cglib method can get the package level / protected method, and the package level / protected method can be called directly through reflection.
The pointcut of the private modifier cannot match method [] methods = clazz getmethods(); Any one here, therefore, cannot be represented. Therefore, @ Autowired cannot be injected because the private method cannot be proxied.
Amendment method:
1. Change the method modifier to public;
2. Use AspectJ for injection.
summary
The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.