Spring Boot Application AOP

Source: Internet
Author: User
Tags aop object object

# Spring Boot Application AOP One, adding dependencies in the POM
<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-aop</artifactId></dependency>
Second, the target class
@RestControllerpublic class StudentController {    @GetMapping(value = "/aoptest")    public String aopTest(){        return " AOP test success!";    }}
Third, Slice class

The code examples are as follows:

@Aspect @configurationpublic class Httpaspect {//Use Org.slf4j.Logger, which is the way spring implements the log private final static Logger Logg    ER = Loggerfactory.getlogger (httpaspect.class); /** * Define the AOP scan path */@Pointcut ("Execution (Public * com.example.myproject.controller.studentcontroller.* ())") pub        LIC void log () {}/** * logs the log at the beginning of the HTTP request */@Before ("log ()") public void Dobefore (Joinpoint joinpoint) {        Servletrequestattributes attributes = (servletrequestattributes) requestcontextholder.getrequestattributes ();        HttpServletRequest request = Attributes.getrequest ();        URL logger.info ("url={}", Request.getrequesturi ());        Method Logger.info ("method={}", Request.getmethod ());        IP logger.info ("ip={}", Request.getremoteaddr ()); Class method Logger.info ("class={} and Method name = {}", Joinpoint.getsignature (). Getdeclaringtypename (), Joinpoint.getsi        Gnature (). GetName ()); Parameter Logger.info ("parameter ={}", Joinpoint.Getargs ()); }/** * Logs the log at the end of the HTTP request */@After ("log ()") public void Doafter () {servletrequestattributes attribute        s = (servletrequestattributes) requestcontextholder.getrequestattributes ();        HttpServletRequest request = Attributes.getrequest ();    Logger.info ("url = {} End of execution", Request.getrequesturl ()); }/** * Gets the contents of the return * * @param object */@AfterReturning (returning = "Object", Pointcut = "log ()") public    void Doafterreturn (Object object) {Logger.info ("response={}", object.tostring ()); }}

PS: Spring aop.xml <aop:config> The connection between the target object, the Pointcut, etc. is configured through the. The sample configuration is as follows:

<aop:config>    <aop:aspect id="log" ref="HttpAspect">        <aop:pointcut id="printLog" expression="execution(* com.example.myproject.controller.StudentController.*())" />        <aop:before method="doBefore" pointcut-ref="printLog" />        <aop:after method="doAfter" pointcut-ref="printLog" />    </aop:aspect></aop:config>
    • Annotation Interpretation

      • @Aspect: Describes a slice class.
      • @Configuration: Describes a configuration class.
      • @Pointcut: Declare a pointcut that determines what the connection points are interested in, and controls when notifications are executed.
      • @Before: A front-facing notification. A notification that is executed before a connection point, but this notification does not block the execution process before the connection point (unless it throws an exception).
      • @AfterFinal Notice. The notification that is executed when a connection point exits (whether it is a normal return or an abnormal exit).
      • @AfterReturning: Post notification. A notification that is executed when a connection point is completed normally, usually when a matching method returns.
    • Pointcut expression

      The format is as follows:

      execution([可见性] 返回类型 [声明类型].方法名(参数) [异常])
      • []: Indicates optional.

      Wildcard characters:

      • *: matches all characters.
      • ..: Typically used to match multiple packages, multiple parameters.
      • +: Represents a class and its subclasses.

Operators: && , || ,!

Iv. Start-up class
@SpringBootApplicationpublic class MyprojectApplication extends SpringBootServletInitializer {    @Override    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {        return application.sources(MyprojectApplication.class);    }    public static void main(String[] args) {        SpringApplication.run(MyprojectApplication.class, args);    }}

PS: The spring-boot default is to start the package of the class as the root path, scanning the current package and the child package.

Spring Boot Application 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.