Spring-boot configuration AOP Gets the parameters in the request in the Controller and their return values

Source: Internet
Author: User

Start by adding AOP dependencies to your maven Pom file:

  <dependency>      <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
    • 1
    • 2
    • 3
    • 4

In spring boot all the configuration is very simple, the following for all my requested controller plus AOP function bar, See code:

Package Me. Jiaobuchong. admin. config; Import Javax. servlet. http. HttpServletRequest; Import org. aspectj. lang. Proceedingjoinpoint; Import org. aspectj. lang. annotation. Around; Import org. aspectj. lang. annotation. Aspect; Import org. aspectj. lang. annotation. Pointcut; Import org. springframework. Context. annotation. Configuration; Import org. springframework. Web. Context. Request. Requestattributes; Import org. springframework. Web. Context. Request. Requestcontextholder; Import org. springframework. Web. Context. Request. Servletrequestattributes; importCom. google. Gson. Gson; Import org. slf4j. Logger; Import org. slf4j. Loggerfactory;;/** * Created by Jiaobuchong on 12/23/15. */@Aspect//define a facet @configurationpublic class Logrecordaspect {private static final Logger Logger = loggerfactory. GetLogger (userinterceptor. Class); Defines the pointcut Pointcut @Pointcut ("execution (* com.jiaobuchong.web.*controller.* (..))") public void Excudeservice () {} @Around ("excudeservice ()") public Object doaround (proceedingjoinpoint Pjp) throws throwable {requestattributes ra = requestconte Xtholder. getrequestattributes (); Servletrequestattributes SRA = (servletrequestattributes) RA; HttpServletRequest request = SRA. getrequest (); String URL = Request. Getrequesturl (). toString (); String method = Request.getmethod () .getrequesturi () ; String queryString = Request.getquerystring ()  logger< Span class= "hljs-preprocessor" >.info ( "request start, individual parameters, url: {}, method: {}, uri: {}, params: {}", ur l, method, uri, QueryString) ;//the value of result is the return value of the intercepted method Object = Pjp.proceed () ; Gson Gson = new Gson () .info (  "request end, Controller return value is" + gson.tojson (result)            
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

Just add the class above, AOP even if configured, do not believe, to visit the following your controller to Try. Compared to the way the AOP was previously configured (xml file), the configuration is now in Java code, @Configuration This annotation is the typical representative of javaconfig, Spring boot will automatically load these configurations at startup, Implement the appropriate configuration Features. This simple small example is a point, I also refer to other People's blog, more details, view the following blog:
http://ysj5125094.iteye.com/blog/2151855

http://blog.csdn.net/jiaobuchong/article/details/50420379

Spring-boot configuration AOP Gets the parameters in the request in the Controller and their return values

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.