• 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

  • 相关阅读:
    程序员式的幽默(灌水)
    你应该知道的
    WPF控件应用[0]
    WPF控件应用[2]
    C#调用Win32 的API函数User32.dll
    C#获取当前行号
    C#导入excel重写
    [转]wpf相关好资源
    使用C#和Excel进行报表开发-生成统计图Chart
    [转]用 delphi 创建一个服务程序
  • 原文地址:https://www.cnblogs.com/lin3615/p/12420334.html
Copyright © 2020-2023  润新知