Preface
Log processing is a very important part of every project. Without logs, the controllability of the system is lost. Without logs, if there is any problem with the system, there will be no trace at all, which is very dangerous for an information system.
The project needs to use log4j logging to the calling process of the class method in the service.
There are many classes and methods in the service, and it is impossible to add the function of log4j logging separately in each class. Therefore, we use the idea of AOP for horizontal slicing here.
Taking the methods in the service class as the entry point, use log4j to output the log before and after the method call through AOP, including the class and method name being called.
During the configuration process, the author encountered some problems in details, both large and small, and is hereby recorded.
First of all, in terms of AOP configuration, we have two options: one is through XML configuration, and the other is through annotation.
XML configuration can be done by the following example:
XML configuration
The corresponding Aspect method does not require the use of @Aspect, @Before and @After annotations.
If you use annotation method, in the XML configuration file, you only need one line to configure it and enable the automatic proxy function:
Automatic proxy
At this time, the corresponding Aspect method can be as follows:
Aspect1
Secondly, it is the pit in the domain to which the Spring configuration file belongs. If the XML file of AOP is configured here, if it needs to be successfully executed, it needs to be
In the web.xml file, add this XML configuration file to the Servlet-Context instead of Spring-Context. The example is as follows:
Global context
The above figure shows the global spring context for configuring, but the aop configuration file cannot be successful here.
Servlet context
We need to load the AOP configuration and put it in the servlet initialization context.
Finally, we run the project, use this method, and view the AOP results:
result
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.