Home > Java > javaTutorial > Detailed explanation of experience sharing using Aspect to implement method entry and exit logging

Detailed explanation of experience sharing using Aspect to implement method entry and exit logging

巴扎黑
Release: 2017-07-18 15:14:17
Original
2287 people have browsed it
@Aspect
public class Aspect
{

    @Before("execution(* com.test.*.*(..))")
    public void logBefore(JoinPoint joinPoint)
    {
        logEnter.....
    }

    @After("execution(* com.test.*.*(..))")
    public void logAfter(JoinPoint joinPoint)
    {
         logExit.....
    }
}
Copy after login

Spring config:

<aop:aspectj-autoproxy/>
    
<bean id="aspect" class="com.test.Aspect"></bean>
Copy after login

Recently I developed a project, which was in a hurry during the entire development process (this is almost the case with every project) ) So in the final stage of the project, it was discovered that the function of recording system logs was missing. In the past, the system was written directly in the code of each module, and then stored in the form, and part of the logs could be viewed on the page. This system does not have this requirement, so I thought of making a system-wide log record. The main recorded data are: operation time, operation object, operation method, some parameters of the operation, and operation results. What I immediately thought of at first was to use aspects to implement aop logging, but in actual applications I found that aspects cannot intercept actions in a friendly way, but are mainly used to intercept service layer businesses. Since the system framework is based on the ssh framework, we finally considered using struts2's built-in interceptor Interceptor.

What interceptor?

The interceptor is an object that dynamically intercepts Action calls. It provides a mechanism that allows developers to define code to be executed before and after an action is executed, and to prevent the execution of an action before it is executed. It also provides a way to extract reusable parts of the action. Speaking of interceptors, there is another word that everyone should know - interceptor chain (Interceptor Chain, called interceptor stack in Struts 2). The interceptor chain is to connect interceptors into a chain in a certain order. When an intercepted method or field is accessed, the interceptors in the interceptor chain will be called in the order they were previously defined.

The principle and general process of the interceptor

1. When ActionInvocation is initialized, all Interceptors related to the Action are loaded according to the configuration.

2. When the Action implementation is called through the ActionInvocation.invoke method, the Interceptor is executed.

Interceptor separates many functions from our Action, greatly reducing the code of our Action, and the independent behavior has good reusability. Many functions of XWork and WebWork are implemented by Interceptor. You can assemble the Interceptor used by Action in the configuration file. It will run before and after Action execution in the order you specify.


The above is the detailed content of Detailed explanation of experience sharing using Aspect to implement method entry and exit logging. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
log
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template