【SpringMVC】之自定义注解

2023-09-15 22:26:43

一、Java注解

1.1 简介

Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。注解相关类都包含在java.lang.annotation包中。

1.2 分类

1.2.1 JDK基本注解

  • @Override 重写
  • @SuppressWarnings(value = “unchecked”) 压制编辑器警告

1.2.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工具提取成文档.

1.3 自定义注解

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

  1. 标记Annotation:没有成员变量的Annotation; 这种Annotation仅利用自身的存在与否来提供信息
  2. 元数据Annotation:包含成员变量的Annotation; 它们可以接受(和提供)更多的元数据

二、使用自定义注解

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.xqx</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.1 案例一(获取类与方法上的注解值)

package com.xqx.annotation.pi;

public enum  TranscationModel {
    Read, Write, ReadWrite
}
package com.xqx.annotation.pi;

import java.lang.annotation.*;

/**
 * MyAnnotation1注解可以用在类、接口、属性、方法上
 * 注解运行期也保留
 * 不可继承
 */
@Target({ElementType.TYPE, ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation1 {
    String name();
}
package com.zking.annotation.pi;

import java.lang.annotation.*;

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

import java.lang.annotation.*;

/**
 * MyAnnotation3注解可以用在方法上
 * 注解运行期也保留
 * 可继承
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MyAnnotation3 {
    TranscationModel[] models() default TranscationModel.ReadWrite;
}

package com.xqx.annotation.pi;

/**
 * 获取类与方法上的注解值
 */
@MyAnnotation1(name = "xqx")
public class Demo1 {

    @MyAnnotation1(name = "xxq")
    private Integer age;

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

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

package com.xqx.annotation.pi;

import org.junit.Test;


public class Demo1Test {
    @Test
    public void list() throws Exception {
//        获取类上的注解
        MyAnnotation1 annotation1 = Demo1.class.getAnnotation(MyAnnotation1.class);
        System.out.println(annotation1.name());//xqx

//        获取方法上的注解
        MyAnnotation2 myAnnotation2 = Demo1.class.getMethod("list").getAnnotation(MyAnnotation2.class);
        System.out.println(myAnnotation2.model());//Read

//        获取属性上的注解
        MyAnnotation1 myAnnotation1 = Demo1.class.getDeclaredField("age").getAnnotation(MyAnnotation1.class);
        System.out.println(myAnnotation1.name());// xxq
    }

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

执行list():
在这里插入图片描述
执行edit():
在这里插入图片描述

2.2 案例二(获取类属性上的注解属性值)

package com.xqx.annotation.p2;

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

/**
 * @author 小李飞刀
 * @site www.javaxl.com
 */
//@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TestAnnotation {
    String value() default "默认value值";

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

package com.xqx.annotation.p2;

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属性对应的值";
}

package com.xqx.annotation.p2;

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

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

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

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

package com.xqx.annotation.p2;

import org.junit.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());
    }
}

执行test1():
在这里插入图片描述
执行test2():
在这里插入图片描述

执行test3():
在这里插入图片描述

2.3 案例三(获取参数修饰注解对应的属性值)

package com.xqx.annotation.p3;

import java.lang.annotation.*;

/**
 * 非空注解:使用在方法的参数上,false表示此参数可以为空,true不能为空
 */
@Documented
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface IsNotNull {
    boolean value() default false;
}

package com.xqx.annotation.p3;

/**
 * 获取参数修饰注解对应的属性值
 */
public class Demo3 {

    public void hello1(@IsNotNull(true) String name) {
        System.out.println("hello:" + name);
    }

    public void hello2(@IsNotNull String name) {
        System.out.println("hello:" + name);
    }
}

package com.xqx.annotation.p3;

import org.junit.Test;

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(annotation.value());//true
            }
        }
    }

    @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(annotation.value());//false
            }
        }
    }

    @Test
    public void hello3() throws Exception {
//        模拟浏览器传递到后台的参数 解读@requestParam
        String name = "zs";
        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);
                }
            }
        }
    }
}

执行hello()1:
在这里插入图片描述
执行hello()2:

在这里插入图片描述

执行hello()3:

在这里插入图片描述

三、Aop自定义注解的应用

AOP(面向切面编程)是一种编程范式,用于将横切关注点(如日志记录、事务管理等)与主要业务逻辑分离。它通过在程序中定义切面(Aspect)来实现这种分离。切面是一组与特定关注点相关的行为,它可以被模块化并应用到多个不同的对象中。AOP的目标是提高代码的模块性、可重用性和可维护性。

pom.xml

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

自定义注解

package com.xqx.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();
}

applicationContext.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">
    <!--1. 注解式开发 -->
    <!-- 注解驱动 -->
    <context:annotation-config/>
    <!-- 用注解方式注入bean,并指定查找范围:com.javaxl.ssh2及子子孙孙包-->
    <context:component-scan base-package="com.xqx"/>
    <!--开启动态代理-->
    <aop:aspectj-autoproxy />

</beans>

应用注解

package com.xqx.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);

    /**
     * 只要用到了com.javaxl.p2.annotation.springAop.MyLog这个注解的,就是目标类
     */
    @Pointcut("@annotation(com.xqx.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());
    }
}

package com.xqx.annotation.aop;

import org.springframework.stereotype.Component;


@Component
public class LogController {

    @MyLog(desc = "这是结合spring aop知识,讲解自定义注解应用的一个案例")
    public void testLogAspect(){
        System.out.println("哈哈哈哈");
    }
}

package com.xqx.annotation.aop;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
// @ContextConfiguration(locations={"classpath:applicationContext.xml"})
@SpringBootTest
public class BaseTestCase {

}

package com.xqx.annotation.aop;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class LogControllerTest extends BaseTestCase {
    @Autowired
    private LogController logController;

    @Test
    public void testLogAspect(){
        logController.testLogAspect();
    }
}

在这里插入图片描述

更多推荐

LVS负载均衡集群

一、集群含义:由多台主机构成,但对外只表现为一一个整体,只提供一个访问入口(域名或IP地址),相当于一台大型计算机。二、群集的类型:1)负载均衡群集LB:提高系统响应效率,处理更多的访问请求,减少延迟,实现高并发、高负载的能力典型代表:软件类:LVSNginxHAProxy等硬件类:F5绿盟2)高可用群集HA:提高系统

docker 存储挂载比较

docker存储概述接触docker的朋友都知道,docker镜像是以layer概念存在的,一层一层的叠加,最终成为我们需要的镜像。但该镜像的每一层都是ReadOnly只读的。只有在我们运行容器的时候才会创建读写层。文件系统的隔离使得:容器不再运行时,数据将不会持续存在,数据很难从容器中取出。无法在不同主机之间很好的进

QT基础教程(QPalette和QIcon)

文章目录前言一、QPalette类二、QIcon类三、QPalette和QIcon之间的转换总结前言本篇文章继续讲解QT中的知识,主要为大家讲解QPalette和QIcon。QPalette和QIcon都是Qt框架中用于图形界面设计的类,它们分别用于管理调色板和图标的相关功能。一、QPalette类QPalette(调

MySQL中的表与视图:解密数据库世界的基石

🏆作者简介,黑夜开发者,CSDN领军人物,全栈领域优质创作者✌,CSDN博客专家,阿里云社区专家博主,2023年6月CSDN上海赛道top4。🏆数年电商行业从业经验,历任核心研发工程师,项目技术负责人。🏆本文已收录于PHP专栏:MySQL的100个知识点。🎉欢迎👍点赞✍评论⭐收藏文章目录🚀一、前言🚀二、基

数据结构与算法(C语言版)P3.1---链表(无头单向非循环链表)

1、链表的概念及结构概念:链表是一种物理存储结构上非连续、非顺序的存储结构。数据元素的逻辑顺序是通过链表中的指针链接次序实现的。注意:​1、从上图可看出:链式结构在逻辑上是连续的,但是在物理上不一定连续。​2、现实中的结点一般都是从堆上申请出来的。​3、从堆上申请空间,是按照一定策略来分配的,再次申请的空间可能连续,不

k8s pod概念、分类及策略

目录一.pod相关概念2.Kubrenetes集群中Pod两种使用方式3.pause容器的Pod中的所有容器共享的资源4.kubernetes中的pause容器主要为每个容器提供功能:6.Pod分为两类:二.Pod容器的分类1.基础容器(infrastructurecontainer)(1)作用(2)配置2.初始化容器

【网络安全】黑客自学笔记

1️⃣前言🚀作为一个合格的网络安全工程师,应该做到攻守兼备,毕竟知己知彼,才能百战百胜。计算机各领域的知识水平决定你渗透水平的上限🚀【1】比如:你编程水平高,那你在代码审计的时候就会比别人强,写出的漏洞利用工具就会比别人的好用;【2】比如:你数据库知识水平高,那你在进行SQL注入攻击的时候,你就可以写出更多更好的S

大数据时代元数据的重要性

元数据,是描述了数据本身(如数据库、数据元素、数据模型),数据表示的概念(如业务流程、应用系统、软件代码、技术基础设施,数据与概念之间的联系。元数据可以帮助组织理解其自身的数据、系统和流程,同时帮助用户评估数据质量,对数据库与其他应用程序的管理来说是不可或缺的。它有助于处理、维护、集成、保护和治理其他数据。在大数据的海

spark 精华总结

面试题:Hadoop的基于进程的计算和Spark基于线程方式优缺点?答案:Hadoop中的MR中每个map/reducetask都是一个java进程方式运行,好处在于进程之间是互相独立的,每个task独享进程资源,没有互相干扰,监控方便,但是问题在于task之间不方便共享数据,执行效率比较低。比如多个maptask读取

Elasticsearch(四)深分页Scroll

一、前言1.1、scroll与from+size区别ES对于from+size的个数是有限制的,二者之和不能超过1w。当所请求的数据总量大于1w时,可用scroll来代替from+size。from+size在ES查询数据的方式步骤如下:1、先将用户指定的关键字进行分词;2、将词汇去分词库中进行检索,得到多个文档的id

数据结构简述,时间、空间复杂度,学习网站推荐

目录IT学习路线相关坚韧大厚书相关有趣/耐看书或视频数据结构与算法学习网站推荐刷题时间、空间复杂度数据结构简述基本概念数据结构与算法简述和CS综述整理。本文非基础的教程,本文会列出大量学习和参考网站。老惯例,一个文章是一个集大成(本文借助了语音输入(PC版讯飞输入法)由此加速码字,但仍保持简洁的文风)。数据结构+算法=

热文推荐