Spring AOP Summary part1

     This paper will make a summary about how to use AOP &  on where we should pay attention。before we start talk about AOP, i should introduce the base environment for our AOP practice.

     Environment Requirements : Spring 4.3.X serial package, Idea_2018 , Java8, Tomcat8.5, if you prepare all those, Here we goes:

     Step 1:add dependecies spring-aop spring-aspects here is the draft of the pom file.



    4.0.0

    palette.song
    gotta
    1.0-SNAPSHOT
    war

    
        
            javax.servlet
            javax.servlet-api
            3.1.0
        
        
            org.springframework
            spring-web
            4.3.2.RELEASE
        

        
            org.springframework
            spring-webmvc
            4.3.2.RELEASE
        

        
            org.springframework
            spring-aop
            4.3.2.RELEASE
        
        
            org.springframework
            spring-aspects
            4.3.2.RELEASE
        

    

     step2: configure the application.xml like this:




    

    


  step3: write the aspect class & set the point cut.   

   

package com.cloud.Aspects;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Aspect
@Service
public class HelloAspects {

    @Pointcut("execution(* com.cloud.song.HelloWorld.sayHello(..))")
    public void pointCut(){

    }

    @Before("pointCut()")
    public void before(JoinPoint joinPoint){
        System.out.println("Hello World! pointCut");
    }

    @AfterThrowing(pointcut = "pointCut()",throwing = "throwable")
    public void afterThrowing(Throwable throwable){

    }

    @After("pointCut()")
    public void after(){

    }

}

step 4 run this web program on tomcat server

Spring AOP Summary part1_第1张图片

when you send a request by a http client like curl or python rquests or a browser you will see a console print, that shows out aspect works. and please notice the project structure and the usages of aop or correctly called aspects. let`s conclude it.

step 5:

How to understand @Pointcut("execution(* com.cloud.song.HelloWorld.sayHello(..))")?well, it the method we will cut or the point we shall cut. it can be accurate to a method or method name contains certain words or the class or a package or even the whole project. if you know a lot about regrex, you can skip this section.

first, the first char * represent the value can be return by the method, '*' tells the aspects every return  method even the void method should included in our point cut , you can assign a precise return to point cut a method. for example (void com.cloud.song.HelloWorld.sayHello(..)), once you do it , the same name method but return int or long or other return type won`t be cut.  by the way, leave a space between * and com

second, the package name & class name & method name, you can replace the by * or . which * replace all the chars & . only replace one chars. but you can`t ingore the split point '.' between packages.  if you want point cut all the method, (* *..*.*(..)))

thrid, also you can composite some point use '||' you can write the point cut path or just write the point cut method. for example

package com.cloud.Aspects;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Aspect
@Service
public class HelloAspects {

    @Pointcut("execution(void *..*.*(..))")
    public void pointCut(){

    }
    
    @Pointcut("execution(int com.*..*(..))")
    public void point(){
        
    }

    @Before("pointCut() || point()")
    public void before(JoinPoint joinPoint){
        System.out.println("Hello World! pointCut");
    }

    @AfterThrowing(pointcut = "pointCut()",throwing = "throwable")
    public void afterThrowing(Throwable throwable){

    }

    @After("pointCut()")
    public void after(){

    }

}

 finally, the (..)  expresses that zero or more params may deliver by the method.

next article i will write the annotions about aop like @after @before and so on. in the serial ending part, i will complain the drawbacks & advantanges about aop on different use situtions.

你可能感兴趣的:(Java,Web,JAVA)