zoukankan      html  css  js  c++  java
  • springboot自定义注解

    系统环境:

    操作系统: win10

    jdk版本:

    openjdk version "12" 2019-03-19
    OpenJDK Runtime Environment (build 12+33)
    OpenJDK 64-Bit Server VM (build 12+33, mixed mode, sharing)

    idea版本:

    IntelliJ IDEA 2020.2.1 (Ultimate Edition)
    Build #IU-202.6948.69, built on August 25, 2020
    Licensed to hello
    Subscription is active until September 11, 2021
    For educational use only.
    Runtime version: 11.0.8+10-b944.31 amd64
    VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
    Windows 10 10.0
    GC: ParNew, ConcurrentMarkSweep
    Memory: 966M
    Cores: 8
    Non-Bundled Plugins: Lombook Plugin, com.intellij.kubernetes, training

    maven版本:3.6.3

    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.3.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.rurjs.example</groupId>
        <artifactId>eureka</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>eureka</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>14</java.version>
            <spring-cloud.version>Hoxton.SR8</spring-cloud.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-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </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>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

    切面类

    package com.rurjs.example.eureka.aspect;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class RjsAspectResponse {
        @Pointcut("@annotation(com.rurjs.example.eureka.aspect.RjsResponse)")
        private void makeResponse() {
    //        return "hi";
        }
    
        /**
         * 定制一个环绕通知
         * @param joinPoint
         * @return
         */
        @Around("makeResponse()")
        public Object advice(ProceedingJoinPoint joinPoint) throws Throwable {
            System.out.println("Around Begin");
            Object res = (String) joinPoint.proceed();//执行到这里开始走进来的方法体(必须声明)
    
            System.out.println("Around End");
            return res;
        }
    
        //当想获得注解里面的属性,可以直接注入该注解
        //方法可以带参数,可以同时设置多个方法用&&
        @Before("makeResponse()")
        public void record(JoinPoint joinPoint) {
            System.out.println("Before");
        }
    
        @After("makeResponse()")
        public void after() {
            System.out.println("After");
        }
    
    }

    注解类

    package com.rurjs.example.eureka.aspect;
    
    import org.springframework.web.bind.annotation.Mapping;
    
    import java.lang.annotation.*;
    
    @Target({ElementType.TYPE, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Mapping
    public @interface RjsResponse {
    
        String value() default "";
    }
  • 相关阅读:
    应用程序框架实战十三:DDD分层架构之我见
    Util应用程序框架公共操作类(三):数据类型转换公共操作类(扩展篇)
    Util应用程序框架公共操作类(二):数据类型转换公共操作类(源码篇)
    不能使用 float 和 double 来表示金额等精确的值
    JVM 字节码指令手册
    MyBatis: Invalid bound statement (not found)错误的可能原因
    Oracle:ORA-01219:database not open:queries allowed on fixed tables/views only
    手写 Spring MVC
    8080 端口被占用的解决方法 netstat -ano;taskkill (命令行)
    Java 工具类 IpUtil
  • 原文地址:https://www.cnblogs.com/tu13/p/springboot_aop.html
Copyright © 2011-2022 走看看