Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An AOP problem is written in the analysis of Java.

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "analyzing an AOP problem in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Today, Xiao A came to a company and found that the company's code is very bad for log processing. All of them are hard-coded output directly in the code, and there is no unified processing. The code is as follows

Student A, immediately thought of, is it possible to use AOP to uniformly operate the log of the project? Just write the code when it comes to it.

Then, after the development was completed, Xiao a carried out a simple test and found that aop was running normally and achieved the effect.

Then Xiao A put the project online, and after launching 1min, suddenly there were many ways to report errors online, resulting in an NPE exception.

Hurry up to roll back the code, and later Xiao A conducted a code search, and found that someone in the project had written the method in controller as private.

The most important thing is that before adding aop, the method works normally, and once aop,UserService is added, it is found that it cannot be injected into the spring container, resulting in NPE.

Analyze the reasons

Spring handles the @ autowire annotation at the time of bean instantiation, before initialization. While the controller class generation agent is generated after bean initialization, although the proxy inherits the properties of the controller object and has the @ autowire annotation, spring no longer handles the @ autowire annotation.

Therefore, the original controller is generated normally in spring, the autowire takes effect, the agent is added to the container later in spring, and the autowire does not take effect.

Because the method of private is not called to the original class by the proxy class, the injected property is not obtained. The public method is OK.

That's why there are the following phenomena

This is the end of the content of "An AOP problem has been written in the Analysis of Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report