Spring-AOP + entry case (annotation) + AOP pointcut syntax + AOP notification type

1. Introduction + Workflow.

Introduction

SpringAop is actually the proxy mode

Aspect = entry point + connection point + notification

work process

2. Import dependencies

1.spring-aop package

This package is a sub-package under spring-context dependency, so if there is context, there will be aop.

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.10.RELEASE</version>
    </dependency>

2.aspectjweaver包

<dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.4</version>
    </dependency>

3. Define interfaces and implementation classes

public interface BookDao {
    public void save();
    public void update();
}

 tip:@Repository annotation is added to the Dao layer, which is the layer that operates with the database.

@Repository
public class BookDaoImpl implements BookDao {

    public void save() {
        System.out.println(System.currentTimeMillis());
        System.out.println("book dao save ...");
    }

    public void update(){
        System.out.println("book dao update ...");
    }
}

4. Define notification classes + create notifications + define entry points

@Component  //告诉spring要把MyAdvice 当作Bean
//设置当前类为切面类类
@Aspect //告诉spring要把MyAdvice 当作AOP,切面类
//定义通知类
public class MyAdvice {
    //设置切入点,要求配置在方法上方
    @Pointcut("execution(void com.itheima.dao.BookDao.update())")
    private void pt(){}

    //设置在切入点pt()的前面运行当前操作(前置通知)
    @Before("pt()")
    //制作共性通知
    public void method(){
        System.out.println(System.currentTimeMillis());
    }
}

As can be seen from the figure, @Aspect tells spring that this is an aop, and @Pointcut represents that the pt() method is an entry point. Note that the py() method only requires an empty shelf  and then @Before ("pt()") represents pt() Insert common methods before methods.

5. Enable the annotation aop function in the spring core configuration

@Configuration
@ComponentScan("com.itheima")
//开启注解开发AOP功能
@EnableAspectJAutoProxy
public class SpringConfig {
}

6. Customized service layer call test

public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        BookDao bookDao = ctx.getBean(BookDao.class);
//        bookDao.update();
        bookDao.update();
//        System.out.println(bookDao.getClass());
    }j
}

7. Detailed explanation of entry points

Pointcut expression syntax specifications:

Tips for writing pointcut expressions:

8. AOP notification types

 1.@After----after the entry point

 2.@Before----before the entry point

3.@Around----around the entry point

ProceedingJoinPoint is used to obtain the object of the original method, and returns the return value of the original method.

 

 4.@AfterReturning

5.@AfterThrowing

Guess you like

Origin blog.csdn.net/m0_61395860/article/details/133132568