1. Introduction
Previous blog wrote the use of AOP for unified Log Management annotated version of the implementation, today write about using XML configuration implementation version, and the previous one is different from the controller layer log we recorded, this time we recorded the service layer log. Use of the project or the original, the specific spring MVC project construction is not introduced. When the controller layer log was recorded, the slice class component was called to Spring MVC for management, because the controller was also assigned to spring MVC for management, but the service layer log should be recorded in the spring container. Because the services layer is managed in the spring container.
2. Implement
(1) First create a new logging class, which is an ordinary class. There are several methods, respectively corresponding to the execution before, after execution, return before, error, surround and so on several need to print the log scene
PackageCOM.LZL.SSS.AOP;Importjava.io.UnsupportedEncodingException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importjava.util.Enumeration;Importjavax.servlet.http.HttpServletRequest;ImportOrg.aspectj.lang.JoinPoint;ImportOrg.aspectj.lang.ProceedingJoinPoint;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;ImportOrg.springframework.web.context.request.RequestContextHolder;Importorg.springframework.web.context.request.ServletRequestAttributes;//Defining Slice Classes Public classServicelogaspect {PrivateLogger Logger = Loggerfactory.getlogger (servicelogaspect.class); //define the notification before the method executes Public voidDobefore (Joinpoint poin)throwsunsupportedencodingexception{Logger.info (Before the "Service" method executes, the current time: "+NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (NewDate ())); Servletrequestattributes attributes=(servletrequestattributes) requestcontextholder.getrequestattributes (); HttpServletRequest Request=attributes.getrequest (); //record the requested contentLogger.info ("Service" Request URL: "+Request.getrequesturl (). toString ()); Logger.info ("Service" Request method: "+Request.getmethod ()); Logger.info ("Service" IP Address: "+request.getremoteaddr ()); Enumeration<String> ENU =Request.getparameternames (); while(Enu.hasmoreelements ()) {String name=(String) enu.nextelement (); Logger.info ("Service" parameter: {}, Value: {} ", name,NewString (Request.getparameter (name). GetBytes ("Iso-8859-1"), "Utf-8")); } } //define the notification, after the method executes Public voidAfter (Joinpoint poin) {logger.info (After the "Service" method executes, the current time: "+NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (NewDate ())); } //defines the notification before the method returns Public voidafterreturning (Joinpoint poin) {logger.info (Before the "Service" method returns, the current time: "+NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (NewDate ())); } //defining notifications, throwing exceptions Public voidafterthrowing (throwable error) {Logger.info ("Service" method error, Current time: "+NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (NewDate ())); } //define notification Wrapping type PublicObject Around (Proceedingjoinpoint PJP)throwsthrowable{Logger.info ("Service" before wrapping: "+NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (NewDate ())); Object obj=pjp.proceed (); Logger.info ("Service" Surround: "+NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (NewDate ())); returnobj; }}
(2) configuration. Inject the log component into spring in the spring configuration file and configure the pointcut expression. Note the new AOP-related section in the header file of the configuration file
<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans /spring-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org /schema/aop/spring-aop-3.0.xsd Http://www.springframework.org/schema/context HTTP://WWW.SPRINGFR Amework.org/schema/context/spring-context-3.0.xsd ">
<BeanID= "Servicelogaspect"class= "Com.lzl.sss.aop.ServiceLogAspect"></Bean> <Aop:config> <Aop:aspectID= "Servicelogaspect"ref= "Servicelogaspect"> <Aop:pointcutexpression= "Execution (* com.lzl.sss.service.*.* (..))"ID= "Businessservice"/> <Aop:beforeMethod= "Dobefore"Pointcut-ref= "Businessservice"/> <Aop:afterMethod= "after"Pointcut-ref= "Businessservice"/> <Aop:aroundMethod= "Around"Pointcut-ref= "Businessservice"/> <aop:after-returningMethod= "Afterreturning"Pointcut-ref= "Businessservice"/> <aop:after-throwingMethod= "Afterthrowing"Pointcut-ref= "Businessservice"throwing= "Error"/> </Aop:aspect> </Aop:config>
3. Achieving results
--xml configuration implementation using AOP for unified log management in Spring MVC