spring aop2

aop:

1.连接点:用于指定具体需要切入的方法路径和方法名。

2.切点:值的是当连接上方法以后的各个通知,每个通知都是一个切点,例如before方法前通知方法,after方法后通知方法

3.切面:就是封装各个切点的类。

4.织入:就是形容某个类的某个方法被连接点连接了,然后就会执行 切面中的各个切点从而执行各个通知方法的过程。

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>aop-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>aop-test</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

2.启动类

package cn.mr.li.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @describe 切面3
 *
 * @author li.yanlong@icjhd.com
 *
 * @date 2021-9-4 23:04:48
 */
@Slf4j
@Aspect
public class MyAspect3 implements Ordered{

    /**
     * <pre>切点</pre> 
     *
     */
    @Pointcut("execution(* cn.mr.li.service.impl.DemoServiceImpl.test(..))")
    public void qieDian() {}
    
    @Before("qieDian()")
    public void testBefore() {
        log.info("MyAspect3 before执行~~~~~~~~~~");
    }
    
    @After("qieDian()")
    public void testAfter() {
        log.info("MyAspect3 after执行~~~~~~~~~");
    }
    
    @AfterReturning("qieDian()")
    public void afterReturning() {
        log.info("MyAspect3 AfterReturning执行~~~~~~");
    }

    //给该对象定个执行顺序
    @Override
    public int getOrder() {
        return 3;
    }
}

 

3.各个切面

package cn.mr.li.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @describe 切面1
 *
 * @author li.yanlong@icjhd.com
 *
 * @date 2021-9-4 23:04:34
 */
@Slf4j
@Aspect
public class MyAspect1 implements Ordered{

    /**
     * <pre>切点</pre> 
     *
     */
    @Pointcut("execution(* cn.mr.li.service.impl.DemoServiceImpl.test(..))")
    public void qieDian() {}
    
    @Before("qieDian()")
    public void testBefore() {
        log.info("MyAspect1 before执行~~~~~~~~~~");
    }
    
    @After("qieDian()")
    public void testAfter() {
        log.info("MyAspect1 after执行~~~~~~~~~");
    }
    
    @AfterReturning("qieDian()")
    public void afterReturning() {
        log.info("MyAspect1 AfterReturning执行~~~~~~");
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
package cn.mr.li.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @describe 切面2
 *
 * @author li.yanlong@icjhd.com
 *
 * @date 2021-9-4 23:04:17
 */
@Slf4j
@Aspect
public class MyAspect2 implements Ordered{

    /**
     * <pre>切点</pre> 
     *
     */
    @Pointcut("execution(* cn.mr.li.service.impl.DemoServiceImpl.test(..))")
    public void qieDian() {}
    
    @Before("qieDian()")
    public void testBefore() {
        log.info("MyAspect2 before执行~~~~~~~~~~");
    }
    
    @After("qieDian()")
    public void testAfter() {
        log.info("MyAspect2 after执行~~~~~~~~~");
    }
    
    @AfterReturning("qieDian()")
    public void afterReturning() {
        log.info("MyAspect2 AfterReturning执行~~~~~~");
    }

    @Override
    public int getOrder() {
        return 2;
    }
}
package cn.mr.li.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @describe 切面3
 *
 * @author li.yanlong@icjhd.com
 *
 * @date 2021-9-4 23:04:48
 */
@Slf4j
@Aspect
public class MyAspect3 implements Ordered{

    /**
     * <pre>切点</pre> 
     *
     */
    @Pointcut("execution(* cn.mr.li.service.impl.DemoServiceImpl.test(..))")
    public void qieDian() {}
    
    @Before("qieDian()")
    public void testBefore() {
        log.info("MyAspect3 before执行~~~~~~~~~~");
    }
    
    @After("qieDian()")
    public void testAfter() {
        log.info("MyAspect3 after执行~~~~~~~~~");
    }
    
    @AfterReturning("qieDian()")
    public void afterReturning() {
        log.info("MyAspect3 AfterReturning执行~~~~~~");
    }

    //给该对象定个执行顺序
    @Override
    public int getOrder() {
        return 3;
    }
}

 

4.service

package cn.mr.li.service;

public interface DemoService {

    void test(String name);
}

 

5.service impl

package cn.mr.li.service.impl;

import org.springframework.stereotype.Service;

import cn.mr.li.service.DemoService;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class DemoServiceImpl implements DemoService {

    @Override
    public void test(String name) {
        log.info("name:{}", name);
    }

}

 

6.controller

package cn.mr.li.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.mr.li.service.DemoService;

@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private DemoService demoService;
    
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test() {
        demoService.test("hello world!!!");
        return "ok";
    }
}

 

启动后访问 http://127.0.0.1:8992/demo/test   

结果:ok

 

posted @ 2021-09-04 23:09  ~~mr.li~~  阅读(53)  评论(0编辑  收藏  举报