转载

Spring AOP

AOP(Aspect Oriented Programming)面向切面的编程

简单点说就是对一个方法执行的特殊点插入一些处理,在这些点执行这些特殊的处理。

例如BookService的showBookInfo方法,我们在该方法的Before,After,Around,AfterReturning,AfterThrowing等特殊点插入执行一些特殊的处理。

几个术语:

连接点(Join point):能够被拦截的地方

切点(Pointcut):处理的方法。即上面提到的showBookInfo方法

增强/通知(advice):添加到切点的处理。上面提到的Before, After, AfterReturning, AfterThrowing

织入(Weaving):将增强/通知添加到具体类的切点的过程

切面(Aspect):有切点和增强/通知组成

BookService.java

public interface BookService {
    public void showBookInfo(Book book);
}

BookServiceImpl.java

@Service
public class BookServiceImpl implements BookService {

    @Override
    public void showBookInfo(Book book) {
        if (book == null) {
            throw new RuntimeException("null object");
        }
        System.out.print("Bookid:"+ book.getID());
        System.out.print("/t BookName:" + book.getName());
        System.out.println("/t Author:" + book.getAuthor());
    }
}

showBookInfo就是我们的切点

BookServiceAspect.java

@Aspect
public class BookServiceAspect {

    @Pointcut("execution(* com.springboot.aop.test.service.BookServiceImpl.showBookInfo(..))")
    public void pointCut() {
        
    }
    
     @Before("pointCut()")
    public void before() {
        System.out.println("-------before.........");
    }
    
     @Around("pointCut()")
     public void around(ProceedingJoinPoint pjp) throws Throwable{
         System.out.println("-------around before.........");
         pjp.proceed();
         System.out.println("-------around after.........");
     }
     
     @After("pointCut()")
    public void after() {
        System.out.println("-------after.........");
    }
     
    @AfterReturning("pointCut()")
    public void afterReturning() {
        System.out.println("-------afterReturning.........");
    }
    
    @AfterThrowing("pointCut()")
    public void afterThrowing() {
        System.out.println("-------afterThrowing.........");
    }
}

对增强/通知添加了对应的处理

BookAOPController.java

@RestController
@RequestMapping("/book")
public class BookAOPController {
    @Autowired
    private BookService bookService = null;

    @ResponseBody
    @GetMapping("/showbookinfo")
    public Book showBookInfo(@RequestParam String bookID, @RequestParam String bookName, @RequestParam String author) {
        Book book = null;

        if (bookID != null) {
            book = new Book();
            book.setID(bookID);
            book.setName(bookName);
            book.setAuthor(author);
        }
        bookService.showBookInfo(book);

        return book;
    }
}

SpringbookAopApplication.java

@SpringBootApplication
public class SpringbootAopApplication {
    
    @Bean(name="bkServiceAspect")
    public BookServiceAspect initAspect() {
        return new BookServiceAspect();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(SpringbootAopApplication.class, args);
    }

}

测试

Spring AOP

Spring AOP

可以看到我们的切点对应的增强/通知中的处理都执行了

示例代码在 SpringbootAOP

原文  https://segmentfault.com/a/1190000021551502
正文到此结束
Loading...