For those of you who don’t know much about SpringAOP, you can check out my previous Spring learning series of blogs. The advent of SpringBoot has greatly lowered the threshold for developers to use Spring. We don’t need to do more configuration, but focus on our business code itself. There are two ways to use AOP in SpringBoot:

Using native SpringAOP (not recommended, but the most basic application)

  • The underlying principle is the third way SpringAOP can be used in web applications, using @aspectj-style annotations

Step 1, introduce Aspect dependencies

	<dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.1</version>
        </dependency>
	<! -- Weaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.1</version>
        </dependency>
Copy the code

Second, start the AspectJ agent in the SpringBoot configuration class

@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class SpringbootLearnApplication {

	public static void main(String[] args) { SpringApplication.run(SpringbootLearnApplication.class, args); }}Copy the code

Step 3: Write code

  • Create a target class
/** ** class */
@Component
public class HighTeacher {
    private String name;
    private int age;

    public void teach(String content) {
        System.out.println("I am a teacher,and my age is " + age);
        System.out.println("Class begins.");
        System.out.println(content);
        System.out.println("Class");
    }

    ...getter and setter
}
Copy the code
  • The aspect class that configures pointcuts and advice
/** * class for pointcuts and notification methods */
@Component
@Aspect
public class AdvisorBean {
    /* pointcut */
    @Pointcut("execution(* teach*(..) )")
    public void teachExecution(a) {}/************ The following is the configuration notification type, which can be multiple ************/
    @Before("teachExecution()")
    public void beforeAdvice(ProceedingJoinPoint joinPoint) {
       Object[] args = joinPoint.getArgs();
        args[0] = "... Your P.E. teacher is sick, so we're starting English.";
        Object proceed = joinPoint.proceed(args);
        
        returnproceed; }}Copy the code
  • The test class
package cn.lyn4ever.learn.springbootlearn;

import cn.lyn4ever.learn.springbootlearn.teacher.HighTeacher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = SpringbootLearnApplication.class)
@RunWith(SpringRunner.class)
public class SpringbootLearnApplicationTests {

    @Autowired
    HighTeacher highTeacher;

	@Test
	public void contextLoads(a) {
        highTeacher.setAge(12);
        highTeacher.teach("Hello everyone, our P.E. teacher, let's start our P.E. class."); }}Copy the code

The result was what everyone wanted. PE class was changed to English class

Use Springboot-start-AOP (recommended)

Introduced in poM files

	<dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-aop</artifactId>  
        </dependency> 
Copy the code

This dependency simply merges steps 1 and 2 of the first method, leaving the rest of the code unchanged