implementing business and Exception logging with spring AOP
AOP is the aspect-oriented programming, which can be used to isolate the various parts of the business logic, thus reducing the coupling of each part of the business logic, improving the reusability of the Code, and improving the development efficiency (from Baidu Encyclopedia).
Actually, it's really good. A recent problem is discovering that the log records in the previous action are not perfect, and that it takes a lot of work to change the logging method on every interface in all the action, which is too large and pointless to think about using AOP. (You can also use interceptors, of course)
Through AOP, all the interfaces in the action are used as pointcuts, the corresponding slices and methods are set, and the return notification is returned after the interface returns, and logging in this notification method reduces the amount of code and facilitates the modification.
Spring AOP has two implementations, one that is configured in Spring-mvc, and one that is implemented by annotations. SPRING-MVC Configuration
<!--enable the AOP--> <aop:aspectj-autoproxy/> <!--AOP Logging method--> <bean id= "busilogaopaction" com. Edf.optrace.core.LogAopAction "/> <!--configuration AOP--> <aop:config> <!--configuring pointcut expressions--> <AOP:POINTC UT id= "busilogpointcut" expression= "Execution (* com.edf.*.controller.*.* (..)) | | Execution (* com.abc.*.controller.*.* (..)) "/> <!--configuration slice and configuration--> <aop:aspect order=" 3 "ref=" Busilogaopac tion "> <!--predecessor notification <aop:before method=" Beformethod "pointcut-ref=" Busilogpointcut "/>
> <!--post notification <aop:after method= "Aftermethod" pointcut-ref= "Busilogpointcut"/>--> <!--return notification--> <aop:after-returning method= "Afterreturnmethod" pointcut-ref= "Busilogpointcut" Returni ng= "Result"/> <!--returns an exception <aop:after-throwing method= "Afterthrowingmethod" pointcut-ref= "Pointcut" Throwing= "ex"/>--> </aop:aspect> </aop:confiG>
public class Logaopaction {@Autowired private ophistoryservice ophistoryservice;
/** * Returns notification (code executed at normal end of method) * Returns the return value that the notification can access to the method.
* @param joinpoit */public void Afterreturnmethod (Joinpoint joinpoint, Object result) {//Get method entry parameter list
list<object> args = Arrays.aslist (Joinpoint.getargs ()); String methodname = Joinpoint.getsignature (). GetName ();//Get method name String ClassName = Joinpoint.gettarget (). GetClass ()
. GetName ()//Get the entity class Requestattributes requestattributes = Requestcontextholder.getrequestattributes ();
HttpServletRequest request = ((servletrequestattributes) requestattributes). Getrequest ();
String params = ""; if (Args!=null && args.size () >0) {//1, processing the incoming parameter in request but not getting the annotation bean enumeration em = Request
. Getparameternames ();
while (Em.hasmoreelements ()) {String name = (String) em.nextelement (); String value = requEst.getparameter (name);
params + = name + "=" + Value + ",";
} params + = ";"; 2, processing the annotations of the bean for (int i = 0; i < args.size (); i++) {try {params
+ + jsonobject.fromobject (args.get (i)) + ";";
catch (Exception e) {e.printstacktrace (); }} String Costtime = (string) request.getsession (). getattribute ("Costtime
"); if (! "". Equals (Stringutil.parsestring (className))) {//Insert business Log ophistoryservice.setoperatehistory (request, C Lassname.substring (Classname.lastindexof (".")
+1), MethodName, params, result.tostring (), costtime);
Request.getsession (). RemoveAttribute ("Costtime"); }
}
}
Annotation Method
/** * Set Exception Log AOP/@Aspect @Order (2) @Component public class Exlogaopaction {@Autowired private Ophistoryservi
CE ophistoryservice; /** * Defines a method that declares a pointcut expression and generally does not need to add additional code * use the @pointcut declaration pointcut Expression * Follow the notification directly using the method name to refer to the current pointcut expression * * * @Pointcu T ("Execution" (* COM.EDF). *.*(..)) || Execution (* com.abc.
*.*(..))") public void Declearjoinpointexpression () {}/** * exception notification (code in which an exception is executed) * You can access the exception object, and you can specify the code to execute when a particular exception occurs * @p Aram Joinpoint * @param ex */@AfterThrowing (value= "declearjoinpointexpression ()", throwing= "ex") public void Afterthrowingmethod (Joinpoint joinpoint, Exception ex) {String methodname = Joinpoint.getsignature (). GetName
();
String entity = Joinpoint.gettarget (). GetClass (). GetName ();
Ophistory opEx = new Ophistory ();
Opex.setdictionaryid ("new");
Opex.setoperatetype ("1");//1 general anomaly; later may add Opex.setoperatename ("Exception"); Opex.setext6 (methodname);//Method name opEX.SETEXT5 (entity);//The Module opex.setupdatecontent (Stringutil.getstringfixedlen (ex.tostring (), 3800));//Exception information content
Insert exception to DB ophistoryservice.insertexerrorhistory (OPEX);
}
}