A simple introduction to AOP annotation configuration

Posted by techevan on Wed, 05 Jan 2022 15:52:12 +0100

A simple introduction to AOP annotation configuration

quick get start

Annotation based aop development steps:

  1. Create target interface and target class (with internal tangent point)
  2. Create facet class (with enhancement method inside)
  3. Leave the object creation rights of the target class and the aspect class to spring
  4. Using annotations to configure weaving relationships in facet classes
  5. Turn on the automatic agent for component scanning and AOP in the configuration file
  6. test
public interface TargetInterface{
    void save();
}
@Component("target")
public class Target implements TargetInterface{
    public void save(){
        System.out.println("save Running...");
    }
}
@Component("myAsect")
@Aspect  //In the table, MyAspect is an aspect class
public class MyAspect{
    //Configure pre enhancement
    @Before(value="execution(* com.jotian.aop.*.*(..))")
    public void before(){
        System.out.println("----------Pre enhancement method--------")
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns: context="http://www.springframework.org/schema/context"
       xmlns: aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!--Component scan-->
    <context:component-scan base-package="com.jotian.aop"/>
    <!--aop Automatic proxy-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-aop:xml")
public class AopText{
    @Autowired
    private TargetInterface target;
    @Test
    public void test1(){
        target.save();
    }
}

Annotation configuration AOP details

Type of annotation notification

Configuration syntax of notification: @ notification annotation ("facet expression")

name annotation explain
Before advice @Before Used to configure pre notification, specifying that the enhanced method is executed before the pointcut method
Post notification @AfterReturning Used to configure post notification, specifying that the enhanced method is executed after the pointcut method
Around Advice @Around Used to configure wrap notifications, specifying that enhanced methods are executed before and after pointcut methods
Exception throw notification @AfterThrowing It is used to configure exception throwing notification and specify that the enhanced method is executed when an exception occurs
Final notice @After Used to configure final notifications. The enhanced mode will be executed regardless of whether there are exceptions

Extraction of tangent expression

Like configuring AOP with xml, we can extract Pointcut expressions. The extraction method is to define a method in the cut plane, define the cut point expression on the method with the @ Pointcut annotation, and then refer to it in the enhanced annotation. The details are as follows

@Component("myAspect")
@Aspect
public class MyAspect{
    @Before("MyAspect.myPoint")
    public void before(){
        System.out.println("Pre code enhancement...")
    }
    @Pointcut("execution(* com.jotian.aop.*.*(..))")
    public void pointcut(){
    }
}
@AfterThrowing("MyAspect.pointcut()")
public void afterThrowing(){
    System.out.println("Exception throw enhancement...")
}
@After("pointcut()")
public void after(){
    System.out.println("Final enhancement...")
}

AOP knowledge points

  • Annotate aop development steps

    1. Dimension facet classes with @ Aspect
    2. Annotate notification method with @ notification annotation
    3. Configure aop auto proxy in the configuration file < aop: AspectJ AutoProxy / >
  • Notification annotation type

    name annotation explain
    Before advice @Before Used to configure pre notification. Specifies that the enhanced method is executed before the pointcut method
    Post notification @AfterReturning Used to configure post notifications. Specifies that the enhanced method is executed after the pointcut method
    Around Advice @Around Used to configure surround notifications. Specifies that the enhanced method is executed before and after the pointcut method
    Exception throw notification @AfterThrowing Used to configure exception throw notifications. Specifies that the enhanced method is executed when an exception occurs
    Final notice @After It is used to configure the final notification. It will be executed regardless of whether there are exceptions in the enhanced mode execution