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.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.aop</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>aop2</name>
    <description>aop2project for Spring Boot</description>

    <properties>
        <java.version>1.8</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-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </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

猜你喜欢

转载自www.cnblogs.com/lin3615/p/12420334.html