Facets of Springboot AOP

Source: Internet
Author: User
Tags aop log4j

Springboot provides powerful AOP support, and we've explained AOP-oriented facets, so here's how the specific AOP principle complements the specific description;

AOP aspect is mainly cut method, we usually engage in some log analysis and transaction operations, to use the plane, similar to interceptors;

@Aspect annotations are facet annotation classes

@Pointcut tangency Definition

@Before is called before the method executes

@After is called after the method executes

Call @AfterReturning method Execution return value

The service layer itself can be cut into transactions, so we have a common method of cutting controller layer

Each method that executes the controller layer records the request URL, the visitor IP executes the class method parameter and other information;

Define a slice class: Requestaspect

importjavax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @Aspect @Component public class RequestAspect {        private Logger logger=Logger.getLogger(RequestAspect. class );           @Pointcut ( "execution(public * com.java1234.controller.*.*(..))" )      public void log(){      }           @Before ( "log()" )      public void deoBefore(JoinPoint joinPoint){          logger.info( "方法执行前..." );          ServletRequestAttributes sra=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();          HttpServletRequest request=sra.getRequest();          logger.info( "url:" +request.getRequestURI());          logger.info( "ip:" +request.getRemoteHost());          logger.info( "method:" +request.getMethod());          logger.info( "class_method:" +joinPoint.getSignature().getDeclaringTypeName()+ "." +joinPoint.getSignature().getName());          logger.info( "args:" +joinPoint.getArgs());      }           @After ( "log()" )      public void doAfter(JoinPoint joinPoint){          logger.info( "方法执行后..." );      }           @AfterReturning (returning= "result" ,pointcut= "log()" )      public void doAfterReturning(Object result){          logger.info( "执行返回值:" +result);      } }

Execution (Public * com.java1234.controller.*.* (..)) This definition means to cut into any class, arbitrary method, arbitrary parameter, or any method of return value under the Com.java1234.controller package.

We test Studentcontroller.

Request: Http://localhost:8888/studentAdd.html

Click "Submit",

Console display:

2017-08-11 11:22:46.357 INFO 9896---[nio-8888-exec-9] com.java1234.aspect.RequestAspect: Before method execution ...

2017-08-11 11:22:46.358 INFO 9896---[nio-8888-exec-9] com.java1234.aspect.requestaspect:url:/student/add

2017-08-11 11:22:46.358 INFO 9896---[nio-8888-exec-9] com.java1234.aspect.requestaspect:ip:0:0:0:0:0:0:0:1

2017-08-11 11:22:46.358 INFO 9896---[nio-8888-exec-9] Com.java1234.aspect.RequestAspect:method:POST

2017-08-11 11:22:46.358 INFO 9896---[nio-8888-exec-9] com.java1234.aspect.RequestAspect:class_method:com.java1 234.controller. Studentcontroller.add

2017-08-11 11:22:46.358 INFO 9896---[nio-8888-exec-9] com.java1234.aspect.requestaspect:args:[ljava.lang.objec t; @fd1ee9

Hibernate:insert to T_student (age, name) values (?,?)

2017-08-11 11:22:46.372 INFO 9896---[nio-8888-exec-9] com.java1234.aspect.RequestAspect: After method execution ...

2017-08-11 11:22:46.373 INFO 9896---[nio-8888-exec-9] com.java1234.aspect.RequestAspect: Execution return value: Add success!

Here we get the information we need;

Of course, the use of the log springboot recommended Logback log4j Upgrade version of the use of almost the same;

Facets of Springboot AOP

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.