spring AOP achieve comment

First, what is AOP

To quote 维基百科the definition

Aspect-oriented programming (Aspect-oriented programming, AOP, also translated as programming aspect-oriented , profile-oriented programming ) is a computer science a medium of program design , aimed at cross-cutting concerns with the business subject for further separation to improve the program code modularity. By adding additional codes on the basis of the existing notification (the Advice) mechanism, able to be declared as a " cut-off point for the unified management of the decorative (Pointcut)" block of code, such as "names of all methods to 'set *' beginning method to add the backlog. " The idea allows developers to code for the core business logic relationships with not-so-close function (such as logging) was added to the program, without compromising the readability of the code of business. Aspect-oriented programming idea is the basis for software development section.

Aspect-oriented programming code logic cut into different modules (i.e., point of interest (Concern), the specified logical function section). Almost all involve programming ideas function classification code, each package will focus into independent abstraction module (such as functions, procedures, modules, classes and methods), which in turn for further achieved, and rewrite the package. Part concern "transverse" program code in several modules, i.e., have appeared in the plurality of modules, i.e. they are referred to as " cross-cutting concerns (Cross-cutting concerns, Horizontal concerns )".

Log function that is a typical case of crosscutting concerns, because the log function is often across the system for each business module, the "cross" all the classes and methods have log body needs. For a credit card applications, deposits, withdrawals, bill management is its core concerns, logs and persistence will be the entire object structure crosscutting crosscutting concerns.

Two, AOP related terms

  • JoinPoint (connection points): a so-called connection points are those points being intercepted. In the spring, these points refers to a method, because only the spring type connection point supporting method.

  • Pointcut (entry point): The so-called entry points means we have to define the intercept which Joinpoint.

  • Advice (notification / Enhanced): The so-called notice refers to Joinpoint after intercepting the thing to do is to inform. Type of notification: notification front, rear notification, abnormality notification, the final notification, around advice.

  • Introduction (introducing): introducing a notification is a special class code without modifying the premise, Introduction class may be dynamically added or Field methods at runtime.

  • Target (Audience): proxy target object.

  • Weaving (weaving): it refers to the enhancement applied to the target object to the process of creating a new proxy object. Agent dynamic spring weaving, while the weaving AspectJ using compile and class loading of weaving.

  • The Proxy (Agent): after being woven into a class enhanced AOP, to produce a result proxy class. -

  • Aspect (section): notification is binding and entry point (introduce) a.


Third, the use annotations to achieve spring AOP

The first is our business logic classes

public class MathCalculator {
    
    public int div(int i,int j){
        System.out.println("MathCalculator...div...");
        return i/j; 
    }

}

Followed by our class section

/**
 * 切面类
 * @author lfy
 * 
 * @Aspect: 告诉Spring当前类是一个切面类
 *
 */
@Aspect
public class LogAspects {
    
    //抽取公共的切入点表达式
    //1、本类引用
    //2、其他的切面引用
    @Pointcut("execution(public int com.atguigu.aop.MathCalculator.*(..))")
    public void pointCut(){};
    
    //@Before在目标方法之前切入;切入点表达式(指定在哪个方法切入)
    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        System.out.println(""+joinPoint.getSignature().getName()+"运行。。。@Before:参数列表是:{"+Arrays.asList(args)+"}");
    }
    
    @After("com.atguigu.aop.LogAspects.pointCut()")
    public void logEnd(JoinPoint joinPoint){
        System.out.println(""+joinPoint.getSignature().getName()+"结束。。。@After");
    }
    
    //JoinPoint一定要出现在参数表的第一位
    @AfterReturning(value="pointCut()",returning="result")
    public void logReturn(JoinPoint joinPoint,Object result){
        System.out.println(""+joinPoint.getSignature().getName()+"正常返回。。。@AfterReturning:运行结果:{"+result+"}");
    }
    
    @AfterThrowing(value="pointCut()",throwing="exception")
    public void logException(JoinPoint joinPoint,Exception exception){
        System.out.println(""+joinPoint.getSignature().getName()+"异常。。。异常信息:{"+exception+"}");
    }
    
        /*@Around(value="pointCut()")
    public void myAround(ProceedingJoinPoint joinPoint) {
        // 方法之前,前置通知
        System.out.println("《方法之前:前置通知");
        try {
            // 方法执行时
            joinPoint.proceed(); // 执行方法
            // 方法执行之后:后置通知
            System.out.println("《方法执行之后:后置通知");
        } catch (Throwable e) {
            // 发生异常时
            System.out.println("《发生异常时:异常通知");
        } finally {
            // 最终通知
            System.out.println("《最终通知");
        }
    }*/

}

Followed by our configuration class:

/**
 * AOP:【动态代理】
 *      指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式;
 * 
 * 1、导入aop模块;Spring AOP:(spring-aspects)
 * 2、定义一个业务逻辑类(MathCalculator);在业务逻辑运行的时候将日志进行打印(方法之前、方法运行结束、方法出现异常,xxx)
 * 3、定义一个日志切面类(LogAspects):切面类里面的方法需要动态感知MathCalculator.div运行到哪里然后执行;
 *      通知方法:
 *          前置通知(@Before):logStart:在目标方法(div)运行之前运行
 *          后置通知(@After):logEnd:在目标方法(div)运行结束之后运行(无论方法正常结束还是异常结束)
 *          返回通知(@AfterReturning):logReturn:在目标方法(div)正常返回之后运行
 *          异常通知(@AfterThrowing):logException:在目标方法(div)出现异常以后运行
 *          环绕通知(@Around):动态代理,手动推进目标方法运行(joinPoint.procced())
 * 4、给切面类的目标方法标注何时何地运行(通知注解);
 * 5、将切面类和业务逻辑类(目标方法所在类)都加入到容器中;
 * 6、必须告诉Spring哪个类是切面类(给切面类上加一个注解:@Aspect)
 * [7]、给配置类中加 @EnableAspectJAutoProxy 【开启基于注解的aop模式】
 *      在Spring中很多的 @EnableXXX;
 * 
 * 三步:
 *  1)、将业务逻辑组件和切面类都加入到容器中;告诉Spring哪个是切面类(@Aspect)
 *  2)、在切面类上的每一个通知方法上标注通知注解,告诉Spring何时何地运行(切入点表达式)
 *  3)、开启基于注解的aop模式;@EnableAspectJAutoProxy
 */
@EnableAspectJAutoProxy
@Configuration
public class MainConfigOfAOP {
     
    //业务逻辑类加入容器中
    @Bean
    public MathCalculator calculator(){
        return new MathCalculator();
    }

    //切面类加入到容器中
    @Bean
    public LogAspects logAspects(){
        return new LogAspects();
    }
}

Finally, we test class:

public class IOCTest_AOP {
    
    @Test
    public void test01(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAOP.class);
        
        //1、不要自己创建对象
//      MathCalculator mathCalculator = new MathCalculator();
//      mathCalculator.div(1, 1);
        MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class);
        
        mathCalculator.div(1, 1);
        
        applicationContext.close();
    }

}

Console print the results:

div运行。。。@Before:参数列表是:{[1, 1]}
MathCalculator...div...
div结束。。。@After
div正常返回。。。@AfterReturning:运行结果:{1}

Comment has been explained in great detail, not repeat them.


Fourth, around advice

Around advice more powerful features, it can achieve the other four notice

Specific code is also very much agree with understanding, open LogAspectsthe myAroundway for the convenience of observation, you can comment other four notifications.

We write a test method test.

This time we divide 0try

mathCalculator.div(1, 0);

print:

《方法之前:前置通知
MathCalculator...div...
《发生异常时:异常通知
《最终通知

Note: Surround notification needs to perform certain methods, parameters ProceedingJoinPoint joinPoint, not the parent class joinPoint.proceed();/ method.

Guess you like

Origin www.cnblogs.com/heliusKing/p/11421135.html