Add log functionality to your system with spring AOP and Java annotations

Source: Internet
Author: User
Tags syslog

Spring AOP has always been a relatively unique feature of spring, and it allows us to embed the logic functions we want anywhere in the existing code without changing our existing code structure.

In view of this, now the system has completed all the functions of the development, we need to record the operation of the system, so as to make it easier to see what some of the actions performed by someone. Spring AOP makes it easy to see which methods, and the corresponding parameters, are performed by someone at some point in the class. However, when most end users look at the names of these methods, they do not know what action the method name code is, and the method description corresponding to the method name needs to be recorded and presented to the user. We know that AOP intercepts certain methods of certain classes, we can get a detailed definition of this method, through the detailed definition, we can get the corresponding annotations of this method, in the annotations we can more convenient to the name and description of the method in writing. As a result, the following annotation definitions are available. The code looks like this:

Add this annotation to the method we need to intercept:

public class Appuseraction extends Baseaction {/** * Add and save action */@Action (description= "Add or Save user Information") Public String Save () {
   
    ....        }       /** * Change Password * @return */@Action (description= "Change password") public String ResetPassword () {              ...        }}
   

Now design our system Log table as follows:

The

Designs embedded logic code, and the following classes are methods that all struts action methods need to be executed in advance. (except for methods for get and set, except for those that do not include an action annotation)

Package Com.htsoft.core.log;import Java.lang.reflect.method;import Java.util.date;import Javax.annotation.Resource ; Import Org.apache.commons.lang.stringutils;import Org.apache.commons.logging.log;import Org.apache.commons.logging.logfactory;import Org.aspectj.lang.proceedingjoinpoint;import Com.htsoft.core.util.contextutil;import Com.htsoft.oa.model.system.appuser;import Com.htsoft.oa.model.system.systemlog;import Com.htsoft.oa.service.system.systemlogservice;public class LogAspect { @Resourceprivate systemlogservice systemlogservice;private Log logger = Logfactory.getlog (logaspect.class);p ublic Object Dosystemlog (Proceedingjoinpoint point) throws Throwable {String methodName = Point.getsignature (). GetName ();// The target method is not NULL if (Stringutils.isnotempty (methodName)) {//set with the Get method except if (!) Methodname.startswith ("set") | | Methodname.startswith ("get")) {Class Targetclass = Point.gettarget (). GetClass (); Method method = Targetclass.getmethod (MethodName); if (Method! = null) {Boolean hasannotation = MethOd.isannotationpresent (Action.class); if (hasannotation) {Action annotation = method.getannotation (Action.class); String METHODDESCP = Annotation.description (), if (logger.isdebugenabled ()) {Logger.debug ("Action method:" + Method.getname () + "Description:" + METHODDESCP);} Take the current operation user Appuser appuser=contextutil.getcurrentuser (); if (appuser!=null) {try{systemlog syslog=new SystemLog (); Syslog.setcreatetime (New Date ()); Syslog.setuserid (Appuser.getuserid ()); Syslog.setusername (Appuser.getfullname () ); Syslog.setexeoperation (METHODDESCP); Systemlogservice.save (SysLog);} catch (Exception ex) {Logger.error (Ex.getmessage ());}}}}} return Point.proceed ();}}

Configure the injection point with AOP:

<aop:aspectj-autoproxy/> <bean id= "Logaspect" class= "Com.htsoft.core.log.LogAspect"/> <aop:config ><aop:aspect ref= "Logaspect" ><aop:pointcut id= "logpointcut" expression= "Execution (* Com.htsoft.oa.action. *(..))" /><aop:around pointcut-ref= "Logpointcut" method= "Dosystemlog"/></aop:aspect></aop:config>

Note that because the default configuration for AOP is to run embedded code using a proxy, and if the strutsaction inherits Actionsupport, the error is caused by its use of the default implementation interface. So the action must be of type Pojo.

If we operate the background of the change password, save the user information operation, the system log will record the following situation.

Add log functionality to your system with spring AOP and Java annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.