springboot2.2的aop切面应用


项目例子结构图

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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.5.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.aopgroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>aop2name>
    <description>aop2project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-aopartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

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

project>

控制器编写

src/main/java/com/example/demo/controller/AopController.java

package com.example.demo.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("aop")
public class AopController
{
    
    @RequestMapping(value="/test")
    public String test()
    {
        System.out.println("yes,it is test");
        return "Hi,lin3615";
    }
    
    @RequestMapping(value="/score/{point}")
    public void score(@PathVariable("point") int point)
    {
        System.out.println("this is score point:"+point);
    }
    

}

编写切面

src/main/java/com/example/demo/aspect/AspectPoint.java

package com.example.demo.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
 * 定义为切面
 * @author lin3615
 *
 */
@Aspect
@Component
public class AspectPoint
{
    /**
     * 定义切入点,即在对应的地方添加行为
     * 如下是在 aop 的控制器中添加上行为
     * 
     * 
     */
    @Pointcut("execution(public * com.example.demo.controller.AopController.*(..))")
    public void aspectPoint()
    {
        
    }
    

    
    @Before("aspectPoint()")
    public void beforeAction()
    {
        System.out.println("new before action ...xx");
    }
    
    @After("aspectPoint()")
    public void afterAction()
    {
        System.out.println("new after action");
    }
    
    @Around("aspectPoint()")
    public void aroundAction(ProceedingJoinPoint pjp) throws Throwable
    {
        try
        {
            System.out.println("around action start");
            pjp.proceed();
            System.out.println("around action end ");
        }catch(Throwable e)
        {
            e.printStackTrace();
        }
    }
}

访问  http://localhost:8080/aop/score/100

控制台输出如下

around action start
new before action ...xx
this is score point:100
around action end
new after action