SpringMVC之自定义注解

目录

一.JAVA注解简介

 1.1.Java注解分类

1.2.JDK元注解

二.自定义注解

1.1.如何自定义注解

 1.2.自定义注解的基本案例

1.2.1.案例一(获取类与方法上的注解值)

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

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

 三.Aop自定义注解的应用

  好啦,今天的分享就到这了,希望能够帮到你呢!😊😊  


一.JAVA注解简介

Java注解是Java语言中一种元数据的形式,它是在程序源代码中插入的特殊标记,用于给程序中的代码提供额外的信息。注解可以用来为类、方法、字段等程序元素添加标记,以表明这些元素应该如何被处理或解析。

Java注解是在编译时或运行时可以被读取和处理的。它们可以被用来提供编译器指令、在运行时进行跟踪和分析、生成代码和文档等。Java注解使用简单的语法进行声明,通常以@符号开头,紧跟注解的名称和一组可选的参数。

Java提供了一些内置的注解,比如@Override用于标记一个方法重写了父类中的方法,@Deprecated用于标记一个已过时的方法或类,@SuppressWarnings用于抑制编译器警告等。此外,开发者也可以自定义注解来满足特定的需求。

通过使用注解,开发者可以为自己的代码添加额外的信息,并通过工具或框架来读取和处理这些注解,以达到特定的目的。注解可以提高代码的可读性、维护性和可扩展性,是Java开发中一个非常有用的特性。

 1.1.Java注解分类

JDK(Java Development Kit)提供了一些基本的注解和元注解,下面分别介绍它们的作用:

1.基本注解:

  • @Override:用于标记一个方法是重写父类中的方法,如果该方法没有正确地重写父类的方法,则会在编译时报错。
  • @Deprecated:用于标记一个方法、类或字段已经过时,表示不推荐使用,编译器会在使用过时元素时发出警告。
  • @SuppressWarnings:用于抑制编译器产生的警告,可以在特定的代码块或者整个方法中使用,让编译器忽略特定的警告。

2.元注解:

  • @Target:用于指定注解可以应用的目标元素类型,如类、字段、方法等。
  • @Retention:用于指定注解的保留策略,即注解在何时生效,包括源代码期、编译期和运行期。
  • @Documented:用于指定注解是否会包含在生成的文档中。
  • @Inherited:用于指定注解是否可以被子类继承。

3.自定义注解:

  • 自定义注解是根据业务需求定义的注解,可以通过@interface关键字来声明,并可以在注解中定义元素和默认值。自定义注解的元素可以是基本类型、枚举、Class类型、String类型以及以上类型的一维数组。自定义注解可以通过@MyAnnotation的形式应用到目标元素上,如类、方法、字段等。可以通过反射机制读取和解析自定义注解,并根据注解的信息进行相应的处理。

使用自定义注解可以增加代码的可读性和可维护性,同时也方便框架和工具对程序进行扩展和定制。

1.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.1.如何自定义注解

要自定义注解,需要使用@interface关键字来声明注解,并在注解中定义元素和默认值。以下是自定义注解的基本步骤:

  1. 使用@interface关键字声明一个注解,如 @interface MyAnnotation

  2. 在注解内部定义元素,并为元素指定默认值。注解的元素可以是基本类型、枚举、Class类型、String类型以及以上类型的一维数组。

  3. 在代码中应用自定义注解。可以在类、方法、字段等目标元素上使用自定义注解。

  4. 通过反射机制读取和解析自定义注解。可以使用Java的反射机制获取类、方法、字段的注解信息,根据注解的信息进行相应的处理。

自定义注解可以根据具体的业务需求灵活定义,并通过反射机制读取和处理注解的信息。注解可以提高代码的可读性、可维护性和可扩展性,是Java开发中一个有用且强大的特性。

 以下是示例,演示了如何自定义一个简单的注解:主要在2.3.4上

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {String value() default "";int count() default 0;
}

在上述例子中,MyAnnotation注解包含两个元素,一个是value,类型为String,默认值为空字符串;另一个是count,类型为int,默认值为0。

 使用了@Retention(RetentionPolicy.RUNTIME)元注解指定了注解在运行时保留,使用了@Target(ElementType.METHOD)元注解指定了注解可以应用在方法上。

MyClass类使用了自定义注解@MyAnnotation,并为注解的元素valuecount指定了具体的值:

@MyAnnotation(value = "Hello", count = 5)
public class MyClass {@MyAnnotation(value = "World")private String name;@MyAnnotation(count = 3)public void myMethod() {// 方法体}
}

 通过getAnnotation()方法获取MyAnnotation注解的实例,然后可以读取注解的元素值。

Class<?> clazz = MyClass.class;
MyAnnotation annotation = clazz.getAnnotation(MyAnnotation.class);
String value = annotation.value();
int count = annotation.count();

 自定义注解可以根据具体的业务需求灵活定义,并通过反射机制读取和处理注解的信息。注解可以提高代码的可读性、可维护性和可扩展性,是Java开发中一个有用且强大的特性。

 1.2.自定义注解的基本案例

创建完项目之后,找到 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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>fzpzyssm</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>fzpzyssm Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><maven.compiler.plugin.version>3.7.0</maven.compiler.plugin.version><!--添加jar包依赖--><!--1.spring 5.0.2.RELEASE相关--><spring.version>5.0.2.RELEASE</spring.version><!--2.mybatis相关--><mybatis.version>3.4.5</mybatis.version><!--mysql--><mysql.version>5.1.44</mysql.version><!--pagehelper分页jar依赖--><pagehelper.version>5.1.2</pagehelper.version><!--mybatis与spring集成jar依赖--><mybatis.spring.version>1.3.1</mybatis.spring.version><!--3.dbcp2连接池相关 druid--><commons.dbcp2.version>2.1.1</commons.dbcp2.version><commons.pool2.version>2.4.3</commons.pool2.version><!--4.log日志相关--><log4j2.version>2.9.1</log4j2.version><log4j2.disruptor.version>3.2.0</log4j2.disruptor.version><slf4j.version>1.7.13</slf4j.version><!--5.其他--><junit.version>4.12</junit.version><servlet.version>4.0.0</servlet.version><lombok.version>1.18.2</lombok.version><mybatis.ehcache.version>1.1.0</mybatis.ehcache.version><ehcache.version>2.10.0</ehcache.version><redis.version>2.9.0</redis.version><redis.spring.version>1.7.1.RELEASE</redis.spring.version><jackson.version>2.9.3</jackson.version><jstl.version>1.2</jstl.version><standard.version>1.1.2</standard.version><tomcat-jsp-api.version>8.0.47</tomcat-jsp-api.version><commons-fileupload.version>1.3.3</commons-fileupload.version><hibernate-validator.version>5.0.2.Final</hibernate-validator.version><shiro.version>1.3.2</shiro.version></properties><dependencies><!--1.spring相关--><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><!--2.mybatis相关--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><!--pagehelper分页插件jar包依赖--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>${pagehelper.version}</version></dependency><!--mybatis与spring集成jar包依赖--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>${mybatis.spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><!--mybatis与ehcache整合--><dependency><groupId>org.mybatis.caches</groupId><artifactId>mybatis-ehcache</artifactId><version>${mybatis.ehcache.version}</version></dependency><!--ehcache依赖--><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId><version>${ehcache.version}</version></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>${redis.version}</version></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>${redis.spring.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>${jackson.version}</version></dependency><!--3.dbcp2连接池相关--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-dbcp2</artifactId><version>${commons.dbcp2.version}</version><exclusions><exclusion><artifactId>commons-pool2</artifactId><groupId>org.apache.commons</groupId></exclusion></exclusions></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>${commons.pool2.version}</version></dependency><!--springmvc依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><!--4.log日志相关依赖--><!-- log4j2日志相关依赖 --><!-- log配置:Log4j2 + Slf4j --><!-- slf4j核心包--><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>${slf4j.version}</version><scope>runtime</scope></dependency><!--核心log4j2jar包--><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId><version>${log4j2.version}</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>${log4j2.version}</version></dependency><!--用于与slf4j保持桥接--><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-slf4j-impl</artifactId><version>${log4j2.version}</version></dependency><!--web工程需要包含log4j-web,非web工程不需要--><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-web</artifactId><version>${log4j2.version}</version><scope>runtime</scope></dependency><!--需要使用log4j2的AsyncLogger需要包含disruptor--><dependency><groupId>com.lmax</groupId><artifactId>disruptor</artifactId><version>${log4j2.disruptor.version}</version></dependency><!--5.其他--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version>
<!--            <scope>test</scope>--></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>${servlet.version}</version><scope>provided</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version><scope>provided</scope></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>${jstl.version}</version></dependency><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>${standard.version}</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-jsp-api</artifactId><version>${tomcat-jsp-api.version}</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>${commons-fileupload.version}</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>${hibernate-validator.version}</version></dependency><!--shiro依赖--><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>${shiro.version}</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-web</artifactId><version>${shiro.version}</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>${shiro.version}</version></dependency></dependencies><build><finalName>fzpzyssm</finalName><resources><!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题--><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource><!--解决mybatis-generator-maven-plugin运行时没有将jdbc.properites文件放入target文件夹的问题--><resource><directory>src/main/resources</directory><includes><include>*.properties</include><include>*.xml</include></includes></resource></resources><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>${maven.compiler.plugin.version}</version><configuration><source>${maven.compiler.source}</source><target>${maven.compiler.target}</target><encoding>${project.build.sourceEncoding}</encoding></configuration></plugin><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.2</version><dependencies><!--使用Mybatis-generator插件不能使用太高版本的mysql驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency></dependencies><configuration><overwrite>true</overwrite></configuration></plugin><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></build>
</project>

1.2.1.案例一(获取类与方法上的注解值)

创建 TranscationModel 直接C到包里接口:

package com.junlinyi.annotation.demo;public enum  TranscationModel {Read, Write, ReadWrite;}

创建 MyAnnotation1 直接C到包里接口

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

 创建 MyAnnotation2 直接C到包里接口

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

 创建 MyAnnotation3 直接C到包里接口

package com.junlinyi.annotation.demo;import java.lang.annotation.*;/*** @author 君临沂* @site www.junlinyi.jly* @company 君氏集团* @create 2023-09-14-18:48** MyAnnotation3注解可以用在方法上* 注解运行期也保留* 可继承*/@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MyAnnotation3 {TranscationModel[] models() default TranscationModel.ReadWrite;
}

 创建测试类进行自定义注解测试 Demo1 

package com.junlinyi.annotation.demo;/*** @author 君临沂* @site www.junlinyi.jly* @company 君氏集团* @create 2023-09-14-19:00** 获取类与方法上的注解值*/@MyAnnotation1(name = "abc")
public class Demo1 {@MyAnnotation1(name = "xyz")public 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");}
}

 创建测试类进行自定义注解测试 Demo1Test

package com.junlinyi.annotation.demo;import org.junit.Test;/*** @author 君临沂* @site www.junlinyi.jly* @company 君氏集团* @create 2023-09-14-19:01*/
public class Demo1Test {@Testpublic void list() throws Exception {
//        获取类上的注解MyAnnotation1 annotation1 = Demo1.class.getAnnotation(MyAnnotation1.class);System.out.println(annotation1.name());//abc//        获取方法上的注解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());// xyz}@Testpublic 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)进行测试,输出结果如下 :

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

场景自定义注解 TestAnnotation

package com.junlinyi.annotation.demo2;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** @author 君临沂* @site www.junlinyi.jly* @company 君氏集团* @create 2023-09-14-19:25*/
//@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TestAnnotation {String value() default "默认value值";String what() default "这里是默认的what属性对应的值";
}

 创建测试类进行自定义注解的测试 Demo2

package com.junlinyi.annotation.demo2;/*** @author 君临沂* @site www.junlinyi.jly* @company 君氏集团* @create 2023-09-14-19:48** 获取类属性上的注解属性值*/
public class Demo2 {@TestAnnotation(value = "这就是value对应的值_msg1", what = "这就是what对应的值_msg1")private static String msg1;//    当没有在注解中指定属性名,那么就是value@TestAnnotation("这就是value对应的值1")private static String msg2;@TestAnnotation(value = "这就是value对应的值2")private static String msg3;@TestAnnotation(what = "这就是what对应的值")private static String msg4;
}

 创建测试类进行自定义注解的测试 Demo2Test

package com.junlinyi.annotation.demo2;import org.junit.Test;/*** @author 君临沂* @site www.junlinyi.jly* @company 君氏集团* @create 2023-09-14-19:48*/
public class Demo2Test {@Testpublic void test1() throws Exception {TestAnnotation msg1 = Demo2.class.getDeclaredField("msg1").getAnnotation(TestAnnotation.class);System.out.println(msg1.value());System.out.println(msg1.what());}@Testpublic void test2() throws Exception{TestAnnotation msg2 = Demo2.class.getDeclaredField("msg2").getAnnotation(TestAnnotation.class);System.out.println(msg2.value());System.out.println(msg2.what());}@Testpublic void test3() throws Exception{TestAnnotation msg3 = Demo2.class.getDeclaredField("msg3").getAnnotation(TestAnnotation.class);System.out.println(msg3.value());System.out.println(msg3.what());}@Testpublic 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 的方法进行测试,输出结果为 : 

 执行其中 test4 的方法进行测试,输出结果为 : 

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

创建自定义注解 IsNotNull 

package com.junlinyi.annotation.demo3;import java.lang.annotation.*;/*** @author 君临沂* @site www.junlinyi.jly* @company 君氏集团* @create 2023-09-14-19:50* * 非空注解:使用在方法的参数上,false表示此参数可以为空,true不能为空*/
@Documented
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface IsNotNull {boolean value() default false;
}

 创建测试类  Demo3

package com.junlinyi.annotation.demo3;/*** @author 君临沂* @site www.junlinyi.jly* @company 君氏集团* @create 2023-09-14-19:50** 获取参数修饰注解对应的属性值*/
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);}
}

 创建测试类  Demo3Test进行方法测试

package com.junlinyi.annotation.demo3;import org.junit.Test;import java.lang.reflect.Method;
import java.lang.reflect.Parameter;/*** @author 君临沂* @site www.junlinyi.jly* @company 君氏集团* @create 2023-09-14-19:50*/
public class Demo3Test {@Testpublic 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}}}@Testpublic 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}}}//    @requestParam
//    默认情况下:传参为空,不会调用方法的,不为空会调用方法
//    requice=false:啥也不传参也会调用方法@Testpublic void hello3() throws Exception {
//        模拟浏览器传递到后台的参数 解读@requestParamString name = "junlinyi";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());//trueif (annotation.value() && !"".equals(name)){method.invoke(demo3,name);}}}}
}

 执行其中的方法( hello1 )进行测试,输出结果为 : 

执行其中的方法( hello2 )进行测试,输出结果为 : 

执行其中的方法( hello3 )进行测试,输出结果为 : 

 

 三.Aop自定义注解的应用

AOP(Aspect-Oriented Programming,面向切面编程)是一种编程范式,可以在程序运行期间通过动态代理方式实现对代码的横切关注点进行模块化管理。其中,自定义注解可以作为AOP的触发器,用于标记需要进行切面处理的目标对象或方法。

以下是一个示例,演示了如何使用自定义注解与AOP结合,实现日志记录的功能:

  1 .定义自定义注解@MyLog,用于标记需要记录日志的方法:

package com.junlinyi.annotation.aop;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** @author 君临沂* @site www.junlinyi.jly* @company 君氏集团* @create 2023-09-14-19:55**/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyLog {String desc();
}

2 . 定义切面类MyLogAspect,在该类中定义增强逻辑,例如记录日志:

package com.junlinyi.aspect;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;import java.util.Arrays;/*** @author 君临沂* @site www.junlinyi.jly* @company 君氏集团* @create 2023-09-14-19:55**/
@Component
@Aspect
public class MyLogAspect {private static final Logger logger = LoggerFactory.getLogger(MyLogAspect.class);/*** 只要用到了com.javaxl.p2.annotation.springAop.MyLog这个注解的,就是目标类*/@Pointcut("@annotation(com.junlinyi.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());
//    }@Around("MyValid()")public Object doAround(ProceedingJoinPoint pjp) throws Throwable {long startTime = System.currentTimeMillis();System.out.println(pjp.getTarget());System.out.println(pjp.getThis());Object[] args = pjp.getArgs();System.out.println(Arrays.toString(args));Object ob = pjp.proceed();// ob 为方法的返回值System.out.println(ob);logger.info("耗时 : " + (System.currentTimeMillis() - startTime));return ob;}
}

在上面的切面类中,使用@Aspect注解标记该类为切面类。@Pointcut("@annotation(com.CloudJun.annotation.aop.MyLog)")注解用于定义切点,表示匹配所有标记有@Log注解的方法。@Before注解表示在目标方法执行前执行增强逻辑。
 

 3 . 创建一个控制器 LogController 

package com.junlinyi.web;import com.junlinyi.annotation.aop.MyLog;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;/*** @author 君临沂* @site www.junlinyi.jly* @company 君氏集团* @create 2023-09-14-19:56**/
@Controller
public class LogController {@RequestMapping("/myLog")@MyLog(desc = "日志管理")public void testLogAspect(HttpServletRequest request) {request.getRemoteAddr();request.getRemotePort();System.out.println("这里随便来点啥");}
}

自定义注解与AOP结合使用,可以实现各种不同的功能,如权限控制、性能监控、事务管理等。根据具体的业务需求,可以定义不同的注解,并在切面类中实现相应的增强逻辑。

测试:

处理中。。。。。

  好啦,今天的分享就到这了,希望能够帮到你呢!😊😊  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/116019.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Qt: 鼠标形状设置

设置全局鼠标形状 设置完毕后&#xff0c;整个APP的任何窗体&#xff0c;包括Dialog中的鼠标形状都会被修改为设定类型&#xff0c;某一个控件设定的鼠标形状将被替换。一般不建议使用 QCursor cursor;//创建鼠标对象 cursor.setShape(Qt::CursorShape::ClosedHandCursor);//…

【初阶数据结构】二叉树全面知识总结

二叉树详解 树的概念及其结构树的概念树的相关概念树的表示方法孩纸兄弟表示法双亲表示法&#xff08;并查集&#xff09; 树的实际应用 二叉树二叉树的概念二叉树的种类二叉树的性质二叉树的存储结构 二叉树顺序结构的实现堆的概念及结构堆向上、向下调整法堆的插入堆的删除堆…

Qt-day3

1、完成文本编辑器的保存工作 //保存按钮对应的槽函数 void Widget::on_saveBtn_clicked() {QString fileName QFileDialog::getSaveFileName(this, //父组件"保存文件", //对话框标题"./", //起始路径"All(*.…

【C语言】指针的进阶(三)—— 模拟实现qsort函数以及指针和数组的笔试题解析

目录 1、模拟实现qsort函数 1.1、qsort函数的回顾 1.2、模拟实现qsort函数 2、指针和数组笔试题解析 2.1、一维数组 2.2、字符数组 1、模拟实现qsort函数 1.1、qsort函数的回顾 要模拟实现qsort函数&#xff0c;就要了解清楚qsort函数的参数以及使用方式。 我们先回顾一…

4 vCPU 实例达成 100 万 JSON API 请求/秒的优化实践

“性能工程” &#xff08;Performance engineering&#xff09;是个日渐流行的概念。顾名思义“性能工程”是包含在系统开发生命周期中所应用的一个技术分支&#xff0c;其目的就是确保满足非功能性的性能需求&#xff0c;例如&#xff1a;性能、可靠性等。由于现代软件系统变…

解决谷歌Redux DevTools调试React+Typescript项目数据对不上/连接不上问题

上文 ReactTypescript项目环境中搭建并使用redux环境 我们创建了一个redux项目的环境 但是我们用谷歌浏览器插件调试 会发现 要不 匹配的数据有问题 看不到数据 要不 就压根连接不到 而且 我们点击加减号 去改变值 调试工具也没有任何反应 我们终端输入 npm install --save-d…

【2023全新保姆级教图文教程】三分钟快速安装好Anacaonda3+Pycharm运行Python

&#x1f60e; 作者介绍&#xff1a;我是程序员洲洲&#xff0c;一个热爱写作的非著名程序员。CSDN全栈优质领域创作者、华为云博客社区云享专家、阿里云博客社区专家博主、前后端开发、人工智能研究生。公粽号&#xff1a;程序员洲洲。 &#x1f388; 本文专栏&#xff1a;本文…

postman发送图片

POSTMAN 如何发送携带图片的请求? 闲话不叙 步骤如下&#xff1a; 新建一个请求&#xff0c;在Headers中添加一对k-v : Content-Type > multipart/form-data 请求的接口: RequestMapping("/fileUploadController")public String fileUpload(MultipartFile fil…

解密堆排序与TopK问题

&#x1f4d9;作者简介&#xff1a; 清水加冰&#xff0c;目前大二在读&#xff0c;正在学习C/C、Python、操作系统、数据库等。 &#x1f4d8;相关专栏&#xff1a;C语言初阶、C语言进阶、C语言刷题训练营、数据结构刷题训练营、有感兴趣的可以看一看。 欢迎点赞 &#x1f44d…

【力扣-每日一题】2560. 打家劫舍 IV

class Solution { public:bool check(vector<int> &nums,int max_num,int k){//只需要计算可以偷的房间。在满足最大值为max_num下时&#xff0c;能偷的最多的房间&#xff0c;与k值比较//如果大于K&#xff0c;说明max_num还可以缩小//如果小于看&#xff0c;说明ma…

Spring Security :二【原理解析、会话管理、RBAC中集成认证和授权、JWT】

文章目录 三、原理解析3.1 结构分析3.1 登录认证流程分析3.1.1 **UserDetailsService**3.1.2 自定义UserDetailsService3.1.3 **PasswordEncoder** 3.2 授权流程分析3.2.1 配置方式的原理解析3.2.2 注解方式原理解析 四、会话管理4.1 获取用户身份4.2 会话控制 五、 RBAC中集成…

20-SpringCloudAlibaba-1

一 Spring Cloud Alibaba简介 什么是Spring Cloud Alibaba Spring Cloud Alibaba致力于提供微服务开发的一站式解决方案。 此项目包含开发分布式应用微服务的必需组件&#xff0c;方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。 为什么要推出Sp…