SpringMVC自定义注解和使用

2023-09-16 05:55:15

一.引言

1.简介:

在Spring MVC中,我们可以使用自定义注解来扩展和定制化我们的应用程序。自定义注解是一种通过Java的注解机制定义的特殊注解,可以应用于控制器类、方法或者方法参数上,以实现不同的功能和行为。(注解相关类都包含在java.lang.annotation包中。)

 2、可实现功能

1.路由映射:可以定义一个自定义注解来标注控制器方法,以指定该方法的URL映射路径。

2.权限控制:可以定义一个自定义注解,用于标记需要进行权限验证的方法,从而实现简单的权限控制逻辑。

3.参数验证:可以定义一个自定义注解,用于标记方法参数,然后结合AOP等技术,在方法执行前或后进行参数的验证和校验。

4.日志记录:可以定义一个自定义注解,用于标记需要进行日志记录的方法,从而实现日志的统一处理。

5.缓存控制:可以定义一个自定义注解,用于标记需要进行缓存管理的方法,从而实现缓存的自动化管理。

 3、使用自定义注解流程

1.定义注解:使用注解语法,在相关的注解类上定义自定义注解,可以指定注解的目标范围和属性。

2.标记使用:在控制器类、方法或者方法参数上使用自定义注解,标识需要应用自定义逻辑的地方。

3.处理注解:通过Spring的AOP、拦截器等机制,对标记了特定自定义注解的类、方法或者参数进行处理,实现相关的功能。

二.java的分解类

1、JDK基本注解

@Override:重写

应用:一般出现在接口实现类里面

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

@SuppressWarnings(value = "unchecked") 

应用:压制编辑器警告

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    String[] value();
}

2、JDK元注解

@Retention:定义注解的保留策略
@Retention(RetentionPolicy.SOURCE)             //注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS)              //默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
@Retention(RetentionPolicy.RUNTIME)            //注解会在class字节码文件中存在,在运行时可以通过反射获取到

@Target:指定被修饰的Annotation可以放置的位置(被修饰的目标)
@Target(ElementType.TYPE)                      //接口、类
@Target(ElementType.FIELD)                     //属性
@Target(ElementType.METHOD)                    //方法
@Target(ElementType.PARAMETER)                 //方法参数
@Target(ElementType.CONSTRUCTOR)               //构造函数
@Target(ElementType.LOCAL_VARIABLE)            //局部变量
@Target(ElementType.ANNOTATION_TYPE)           //注解
@Target(ElementType.PACKAGE)                   //包
注:可以指定多个位置,例如:
@Target({ElementType.METHOD, ElementType.TYPE}),也就是此注解可以在方法和类上面使用

@Inherited:指定被修饰的Annotation将具有继承性

@Documented:指定被修饰的该Annotation可以被javadoc工具提取成文档.

 3、自定义注解

注解分类(根据Annotation是否包含成员变量,可以把Annotation分为两类):

标记Annotation:
没有成员变量的Annotation; 这种Annotation仅利用自身的存在与否来提供信息

元数据Annotation:
包含成员变量的Annotation; 它们可以接受(和提供)更多的元数据;

4、如何自定义注解 ?

使用@interface关键字, 其定义过程与定义接口非常类似, 需要注意的是:
   Annotation的成员变量在Annotation定义中是以无参的方法形式来声明的, 其方法名和返回值类型定义了该成员变量的名字和类型,
   而且我们还可以使用default关键字为这个成员变量设定默认值;

 三.使用自定义注解

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.3.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xzs</groupId>
    <artifactId>spboottest01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spboottest01</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>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-test</artifactId>
<!--            <scope>test</scope>-->
        </dependency>
    </dependencies>

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

</project>

2.了解什么是枚举?

   枚举类是Java中一种特殊的数据类型,用于定义一组固定的命名常量。枚举类型提供了一种更强大、更安全和更易读的方式来表示一组相关的常量。在Java中,枚举类型是通过关键字enum来定义的。

作用:枚举类的作用是提供一种类型安全的方式来表示一组固定的值,避免使用数字或字符串常量时可能出现的错误。通过使用枚举类型,可以限制变量只能取预定义的值,并且可以在代码中使用这些常量的名称来提高可读性和可维护性。

package com.xzs.annotation;

public enum  TranscationModel {
    Read, Write, ReadWrite
}

 3、自定义注解类

1.MyAnnotation1 
package com.xzs.annotation.pi;

import java.lang.annotation.*;

@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation1 {
    int id();

    String name();
}

2、MyAnnotation2

package com.xzs.annotation.pi;

import com.xzs.annotation.TranscationModel;

import java.lang.annotation.*;
 
/**
 *  MyAnnotation2注解可以用在方法上
 *  注解运行期也保留
 *  不可继承
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation2 {
    TranscationModel model() default TranscationModel.ReadWrite;

}

 .3、MyAnnotation3

package com.xzs.annotation.pi;

import com.xzs.annotation.TranscationModel;

import java.lang.annotation.*;
 
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MyAnnotation3 {
    TranscationModel[] models() default TranscationModel.ReadWrite;


}

.4、TestAnnotation

package com.xzs.annotation.pi;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
//@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TestAnnotation {
    String value() default "默认value值";

    String what() default "这里是默认的what属性对应的值";

}

案例1:

1.创建一个测试类

package com.xzs.annotation.pi;

import com.xzs.annotation.TranscationModel;

@MyAnnotation1(id = 1, name = "lei")
public class DemoTest {

    @MyAnnotation1(id = 2, name = "sx")
    private Integer age;

    @MyAnnotation2(model = TranscationModel.ReadWrite)
    public void select() {
        System.out.println("select");
    }

    @MyAnnotation3(models = {TranscationModel.Read, TranscationModel.Write, TranscationModel.ReadWrite})
    public void up() {
        System.out.println("up");
    }

}

 2、获取类上的注解

    @Test
    public void list() throws Exception {
//        获取类上的注解
        MyAnnotation1 annotation1 = DemoTest.class.getAnnotation(MyAnnotation1.class);
        System.out.println(annotation1.name() + "/" + annotation1.id());
}

 3.获取方法上的注解

 @Test
    public void list2() throws Exception {
//        获取方法上的注解
        MyAnnotation2 myAnnotation2 = DemoTest.class.getMethod("select").getAnnotation(MyAnnotation2.class);
        System.out.println(myAnnotation2.model());

    }

 

4. 获取属性上的注解

 @Test
    public void list3() throws Exception {
//        获取属性上的注解
        MyAnnotation1 myAnnotation1 = DemoTest.class.getDeclaredField("age").getAnnotation(MyAnnotation1.class);
        System.out.println(myAnnotation1.name() + "/" + myAnnotation1.id());
    }

 .5、遍历方法上的注解

  @Test
    public void edit() throws Exception {
        MyAnnotation3 myAnnotation3 = DemoTest.class.getMethod("up").getAnnotation(MyAnnotation3.class);
        for (TranscationModel model : myAnnotation3.models()
        ) {
            System.out.println(model);
        }
    }

案例2:

 1.测试类:

package com.xzs.annotation.p2;

import com.xzs.annotation.pi.TestAnnotation;

public class Demo2 {
    @TestAnnotation(value = "这就是value对应的值_msg1", what = "这就是what对应的值_msg1")
    private static String msg1;

    @TestAnnotation("这就是value对应的值1:msg2")
    private static String msg2;

    @TestAnnotation(value = "这就是value对应的值2:msg3")
    private static String msg3;

    @TestAnnotation(what = "这就是what对应的值:msg4")
    private static String msg4;

}

package com.xzs.annotation.p2;

import com.xzs.annotation.pi.TestAnnotation;
import org.junit.jupiter.api.Test;


public class Demo2Test {
    @Test
    public void test1() throws Exception {
        TestAnnotation msg1 = Demo2.class.getDeclaredField("msg1").getAnnotation(TestAnnotation.class);
        System.out.println(msg1.value());
        System.out.println(msg1.what());
    }
 
    @Test
    public void test2() throws Exception{
        TestAnnotation msg2 = Demo2.class.getDeclaredField("msg2").getAnnotation(TestAnnotation.class);
        System.out.println(msg2.value());
        System.out.println(msg2.what());
    }
 
    @Test
    public void test3() throws Exception{
        TestAnnotation msg3 = Demo2.class.getDeclaredField("msg3").getAnnotation(TestAnnotation.class);
        System.out.println(msg3.value());
        System.out.println(msg3.what());
    }
 
    @Test
    public void test4() throws Exception{
        TestAnnotation msg4 = Demo2.class.getDeclaredField("msg4").getAnnotation(TestAnnotation.class);
        System.out.println(msg4.value());
        System.out.println(msg4.what());
    }
}

 

 

 

 案例3:

自定义注解

package com.xzs.annotation.p3;

import java.lang.annotation.*;
 
@Documented
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface IsNotNull {
    boolean value() default false;

}

测试类

package com.xzs.annotation.p3;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class Demo3Test {
 
    @Test
    public void hello1() throws Exception {
        Demo3 demo3 = new Demo3();
        for (Parameter parameter : demo3.getClass().getMethod("hello1", String.class).getParameters()) {
            IsNotNull annotation = parameter.getAnnotation(IsNotNull.class);
            if (annotation != null) {
                System.out.println("hello1:" + annotation.value());//true
                System.out.println(("-----------hello1----------"));
            }
        }
    }
 
    @Test
    public void hello2() throws Exception {
        Demo3 demo3 = new Demo3();
        for (Parameter parameter : demo3.getClass().getMethod("hello2", String.class).getParameters()) {
            IsNotNull annotation = parameter.getAnnotation(IsNotNull.class);
            if (annotation != null) {
                System.out.println("hello2:" + annotation.value());//false
                System.out.println(("-----------hello2----------"));
            }
        }
    }
 
    @Test
    public void hello3() throws Exception {
//        模拟浏览器传递到后台的参数 解读@requestParam
        String name = "tqg";
        Demo3 demo3 = new Demo3();
        Method method = demo3.getClass().getMethod("hello1", String.class);
        for (Parameter parameter : method.getParameters()) {
            IsNotNull annotation = parameter.getAnnotation(IsNotNull.class);
            if (annotation != null) {
                System.out.println(annotation.value());//true
                if (annotation.value() && !"".equals(name)) {
                    method.invoke(demo3, name);
                }
                System.out.println(("-----------hello3----------"));  }
        }
    }}

 

四、Aop自定义注解应用

1、自定义注解

package com.xzs.annotation.aop;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyLog {
    String desc();
}

 2、配置spring-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!--spring框架与mybatis整合配置文夹加载到spring上下文中-->
    <import resource="spring-mybatis.xml"></import>
   <context:annotation-config/>
    <!-- 用注解方式注入bean,并指定查找范围:com.javaxl.ssh2及子子孙孙包-->
    <context:component-scan base-package="com.xzs"/>
    <!--开启动态代理-->
    <aop:aspectj-autoproxy/>
</beans>

3、应用注解

package com.xzs.annotation.aop;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;



@Component
@Aspect
public class MyLogAspect {
    private static final Logger logger = LoggerFactory.getLogger(MyLogAspect.class);
 
   
    @Pointcut("@annotation(com.xzs.annotation.aop.MyLog)")
    private void MyValid() {
    }
 
    @Before("MyValid()")
    public void before(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        logger.debug("[" + signature.getName() + " : start.....]");
        System.out.println("[" + signature.getName() + " : start.....]");
 
        MyLog myLog = signature.getMethod().getAnnotation(MyLog.class);
        logger.debug("【目标对象方法被调用时候产生的日志,记录到日志表中】:" + myLog.desc());
        System.out.println("【目标对象方法被调用时候产生的日志,记录到日志表中】:" + myLog.desc());
    }
}

 4、Controller层代码

package com.xzs.web;

import com.xzs.annotation.aop.MyLog;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

@Component
@RequestMapping("/log")
public class LogController {

    @RequestMapping("/log")
    @MyLog(desc = "这是结合spring aop知识,讲解自定义注解应用的一个案例")
    public void testLogAspect() {
        System.out.println("这里随便来点啥,最好是唱跳rap");
    }

}

 结果:

更多推荐

数据结构——堆

堆文章目录堆1.什么是堆2.堆的两个特性3.父子节点下标之间的关系4.堆的实现4.1堆的插入HeapPush4.1.1向上调整算法AdjustUp:4.1.2AdjustUp代码实现(以大堆为例)4.1.3HeapPush实现4.1.4时间复杂度4.2堆的删除HeapPop4.2.1向下调整算法AdjustDown4.

基于Docker的JMeter分布式压测实战讲解

一个JMeter实例可能无法产生足够的负载来对你的应用程序进行压力测试。如本网站所示,一个JMeter实例将能够控制许多其他的远程JMeter实例,并对你的应用程序产生更大的负载。JMeter使用JavaRMI[远程方法调用]来与分布式网络中的对象进行交互。JMeter主站和从站的通信如下图所示:我们需要为每个Slav

Hive内置函数字典

写在前面:HQL同SQL有很多的类似语法,同学熟悉SQL后一般学习起来非常轻松,写一篇文章列举常用函数,方便查找和学习。1.执行模式1.1BatchMode批处理模式当使用-e或-f选项运行$HIVE_HOME/bin/hive时,它将以批处理模式执行SQL命令。所谓的批处理可以理解为一次性执行,执行完毕退出#-e$H

贝叶斯分位数回归、lasso和自适应lasso贝叶斯分位数回归分析免疫球蛋白、前列腺癌数据...

原文链接:http://tecdat.cn/?p=22702贝叶斯回归分位数在最近的文献中受到广泛关注,本文实现了贝叶斯系数估计和回归分位数(RQ)中的变量选择,带有lasso和自适应lasso惩罚的贝叶斯(点击文末“阅读原文”获取完整代码数据)。摘要还包括总结结果、绘制路径图、后验直方图、自相关图和绘制分位数图的进一

区块链:去中心化革命下的创新与发展!

区块链作为一项重要的技术实验,确实具有重大的影响力。它代表了一场去中心化的运动,吸引了许多研究人员、工程师、建设者和用户的参与,创造了我们目前所见到的一些最有趣的技术。区块链的透明特性赋予了用户对于数据和交易的控制权,保护了用户的匿名性,同时也提供了交易结算状态和服务运作的清晰度。用户可以选择参与区块链网络,并获得相应

2023年电赛---运动目标控制与自动追踪系统(E题)OpenMV方案

如果有嵌入式企业需要招聘校园大使,湖南区域的日常实习,任何区域的暑假Linux驱动实习岗位,可C站直接私聊,或者邮件:zhangyixu02@gmail.com,此消息至2025年1月1日前均有效前言(1)废话少说,很多人可能无法访问GitHub,所以我直接贴出可能要用的代码。此博客还会进行更新,先贴教程和代码(2)<

14:00面试,14:06就出来了,问的问题有点变态。。。

从小厂出来,没想到在另一家公司又寄了。到这家公司开始上班,加班是每天必不可少的,看在钱给的比较多的份上,就不太计较了。没想到5月一纸通知,所有人不准加班,加班费不仅没有了,薪资还要降40%,这下搞的饭都吃不起了。还在有个朋友内推我去了一家互联网公司,兴冲冲见面试官,没想到一道题把我给问死了:如果模块请求http改为了h

消息队列——rabbitmq的不同工作模式

目录Workqueues工作队列模式Pub/Sub订阅模式Routing路由模式Topics通配符模式工作模式总结Workqueues工作队列模式C1和C2属于竞争关系,一个消息只有一个消费者可以取到。代码部分只需要用两个消费者进程监听同一个队里即可。两个消费者呈现竞争关系。用一个生产者推送10条消息for(inti=

什么是线程?为什么需要线程?和进程的区别?

目录前言一.线程是什么?1.1.为什么需要线程1.2线程的概念1.3线程和进程的区别二.线程的生命周期三.认识多线程总结🎁个人主页:tq02的博客_CSDN博客-C语言,Java,Java数据结构领域博主🎥本文由tq02原创,首发于CSDN🙉🎄本章讲解内容:线程的讲解🎥学习专栏:C语言JavaSEMySQL基

2023年电赛---运动目标控制与自动追踪系统(E题)OpenART mini的代码移植到OpenMV

如果有嵌入式企业需要招聘校园大使,湖南区域的日常实习,任何区域的暑假Linux驱动实习岗位,可C站直接私聊,或者邮件:zhangyixu02@gmail.com,此消息至2025年1月1日前均有效前言(1)已经有不少同学根据我上一篇博客完成了前三问,恭喜恭喜。有很多同学卡在了第四问。(2)我说了OpenARTmini的

React、Vue框架如何实现组件更新,原理是什么?

引言React和Vue都是当今最流行的前端框架,它们都实现了组件化开发模式。为了优化性能,两者都采用了虚拟DOM技术。当组件状态发生改变时,它们会使用虚拟DOM进行局部渲染比对,只更新必要的DOM节点,从而避免重新渲染整个组件树。本文将从React和Vue的组件更新原理入手,剖析两者虚拟DOMdifer算法的异同点。R

热文推荐