Using the SSM to record each action record (the same as the operation log) with SPRINGAOP slice and custom note

Source: Internet
Author: User
Tags aop

Some time ago according to business requirements to record the relevant information of each operation: this way through the SPRINGMVC in the controller set a custom annotation class to receive the data, through the SPRINGAOP section to the received data for logical processing.

Based on an entity class (such as: Uploadcount) that is created in advance based on business requirements:

1. First Springmvc.xml to configure

<!--start AOP aspectj annotations automatic proxy--> 
<aop:aspectj-autoproxy proxy-target-class= "true"/>
<!-- Specify the path and configuration of the custom annotation class to add a custom annotation on the controller to receive the data--> 
<context:component-scan base-package= " Com.sl.api.common.util.aopUtils ">
       <context:include-filter type=" annotation "expression=" Org.springframework.stereotype.Controller "/>
</context:component-scan>
2. Create custom annotation classes based on business requirements

@Retention (retentionpolicy.runtime)  
@Target (elementtype.method)
@Documented public
@interface uploadcountlogs {
	String modulename () Default "";   Custom Parameters
}
3. Create a pointcut class that logically handles incoming data

/** * Pointcut class * @author Shilei * @version 1.0/@Aspect @Component public class uploadcountlogsaspect{
	The injection service is used to save the relevant data information of the uploaded record to the database @Autowired private Schemeproposalsservice schemeproposalsservice;
	
	@Autowired private Userredisutils userredisutils;

	Local Exception Log Record object//private static final Logger Logger = Loggerfactory.getlogger (Uploadcountlogsaspect. Class); Controller-level tangent @Pointcut ("@annotation (com.sl.api.common.util.aopUtils.UploadCountLogs)") public void
	Controlleraspect () {System.out.println ("pointcut ...");     
	/** * Forward notifications are used to intercept controller layer fetch record operation information * * @param joinpoint pointcuts/@Before ("Controlleraspect ()") public void Dobefore (Joinpoint joinpoint) {httpservletrequest request = (servletrequestattributes) REQUESTC    
		Ontextholder.getrequestattributes ()). Getrequest (); 
		User user = Userredisutils.getuser (request);
try {//specific business logic changes according to business requirements Uploadcount count = new Uploadcount ();			Count.settid (Uuid.randomuuid (). toString ());
			Count.setmodulename (Getcontrollermethoddescription (Joinpoint) [0]);
			Count.setcreateuid (User.getuserid (). toString ());
			Count.setdeptid (User.getdeptid ());
			Count.setdeptname (User.getdept ());
		Save Database Schemeproposalsservice.addrecord (count);
		catch (Exception e) {e.printstacktrace ();  /** * Gets the description of the method in the annotation for the controller layer annotation * * @param joinpoint Pointcut * @return Method Description * @throws    
		Exception */public static string[] Getcontrollermethoddescription (Joinpoint joinpoint) throws Exception {    
		String targetName = Joinpoint.gettarget (). GetClass (). GetName ();    
		String methodname = Joinpoint.getsignature (). GetName ();    
		object[] arguments = Joinpoint.getargs ();    
		Class Targetclass = Class.forName (targetName);   
		Method[] methods = Targetclass.getmethods ();
		String[] Annos = {""};   For (method Method:methods) {if (Method.getname (). Equals (methodname)) { 
				class[] Clazzs = Method.getparametertypes (); 
				     if (clazzs.length = = arguments.length) {Annos[0] = method.getannotation (uploadcountlogs.class). ModuleName ();
					The specific business logic changes according to business requirements if (Annos[0].equals ("News")) {Random Random = new Random ();  int ran = random.nextint (30);
					[0,30) if (ran<10) {annos[0]= "Public affairs publicity";
					}else if (ran>=10 && ran<20) {annos[0]= "Government public opinion";
					}else {annos[0]= "news data";    
			     }} break;    
	}} return annos; }    
}

4. Finally, add the custom annotation class to the controller, complete the!!!

       @UploadCountLogs (modulename= "News")




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.