In project development, it is often necessary to log some user-operated system logs to the database, not just records in files or log4j.
The first is the simplest, the most primitive and the most cumbersome and the most stupid way: each need to record the operation of the entry method to call the new log interface.
The second is to use spring interceptors for method interception:
Create an Interceptor:
<mvc:interceptor>
<mvc:mapping path= "/**"/>
<bean class= "COM.DW. Interceptor. Loginterceptor "></bean>
</mvc:interceptor>
The class of the new interceptor inherits the spring Web's Handlerinterceptoradapter class, which has four methods that can be overridden in Spring4, such as:
Prehandle: It executes before processing, and can be used to do things like encoding processing, security restrictions, and so on.
Posthandle: It is performed before the method executes after it has been executed, and can be logged, modified, Modelview, and so on.
Aftercompletion: The last execution, whether error or not, will execute this method, can be used to record the exception information and some necessary operational records.
The Afterconcurrenthandlingstarted:controller method executes this method asynchronously when it starts executing, and posthandle needs to wait until the controller executes asynchronously.
It is important to note that the spring interceptor cannot get the parameter value of the handler function.
The third is to use spring's AOP configuration annotations to intercept:
First of the three concepts in SPRINGAOP: advice, pointcut, advisor.
Create a new class without any inheritance and implementation interface, simply add the annotation @aspect to the class. Create a method for a pointcut, annotate @pointcut, and then create a method that configures the type that needs to be notified by Joinpoint the related class to get the parameter value and the requested content. The specific pointcut and notification type expressions need to refer to the SPRINGAOP-related expression syntax. Then you need to configure <AOP:ASPECTJ-AUTOPROXY> in the configuration file, that is, ASPECTJ dynamic agent.
Specific logging implementations can be recorded using a method that matches the requested address (because the query operation may not need to be logged), which requests that need to be logged can be configured through a configuration file, or can be implemented by annotations.
or through custom annotations to achieve log interception, by pointcut interception of specific annotations to achieve the function of logging on demand.
Springmvc several ways to record system logs