SpringAOP02 自定义注解

 

1 自定义注解

  1.1 创建自定义注解

    从java5开始就可以利用 @interface 来定义自定义注解

    技巧01:注解不能直接干扰程序代码的运行(即:注解的增加和删除操作后,代码都可以正常运行)

    技巧02:@Retention 用来声明注解的保留期限

/** Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.*********************/package java.lang.annotation;/*** Annotation retention policy.  The constants of this enumerated type* describe the various policies for retaining annotations.  They are used* in conjunction with the {@link Retention} meta-annotation type to specify* how long annotations are to be retained.** @author  Joshua Bloch* @since 1.5*/
public enum RetentionPolicy {/*** Annotations are to be discarded by the compiler.*/SOURCE,/*** Annotations are to be recorded in the class file by the compiler* but need not be retained by the VM at run time.  This is the default* behavior.*/CLASS,/*** Annotations are to be recorded in the class file by the compiler and* retained by the VM at run time, so they may be read reflectively.** @see java.lang.reflect.AnnotatedElement*/RUNTIME
}
RetentionPolicy.java

    技巧03:@Target 用来声明使用该注解的目标类型

/** Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.*********************/package java.lang.annotation;/*** The constants of this enumerated type provide a simple classification of the* syntactic locations where annotations may appear in a Java program. These* constants are used in {@link Target java.lang.annotation.Target}* meta-annotations to specify where it is legal to write annotations of a* given type.** <p>The syntactic locations where annotations may appear are split into* <em>declaration contexts</em> , where annotations apply to declarations, and* <em>type contexts</em> , where annotations apply to types used in* declarations and expressions.** <p>The constants {@link #ANNOTATION_TYPE} , {@link #CONSTRUCTOR} , {@link* #FIELD} , {@link #LOCAL_VARIABLE} , {@link #METHOD} , {@link #PACKAGE} ,* {@link #PARAMETER} , {@link #TYPE} , and {@link #TYPE_PARAMETER} correspond* to the declaration contexts in JLS 9.6.4.1.** <p>For example, an annotation whose type is meta-annotated with* {@code @Target(ElementType.FIELD)} may only be written as a modifier for a* field declaration.** <p>The constant {@link #TYPE_USE} corresponds to the 15 type contexts in JLS* 4.11, as well as to two declaration contexts: type declarations (including* annotation type declarations) and type parameter declarations.** <p>For example, an annotation whose type is meta-annotated with* {@code @Target(ElementType.TYPE_USE)} may be written on the type of a field* (or within the type of the field, if it is a nested, parameterized, or array* type), and may also appear as a modifier for, say, a class declaration.** <p>The {@code TYPE_USE} constant includes type declarations and type* parameter declarations as a convenience for designers of type checkers which* give semantics to annotation types. For example, if the annotation type* {@code NonNull} is meta-annotated with* {@code @Target(ElementType.TYPE_USE)}, then {@code @NonNull}* {@code class C {...}} could be treated by a type checker as indicating that* all variables of class {@code C} are non-null, while still allowing* variables of other classes to be non-null or not non-null based on whether* {@code @NonNull} appears at the variable's declaration.** @author  Joshua Bloch* @since 1.5* @jls 9.6.4.1 @Target* @jls 4.1 The Kinds of Types and Values*/
public enum ElementType {/** Class, interface (including annotation type), or enum declaration */TYPE,/** Field declaration (includes enum constants) */FIELD,/** Method declaration */METHOD,/** Formal parameter declaration */PARAMETER,/** Constructor declaration */CONSTRUCTOR,/** Local variable declaration */LOCAL_VARIABLE,/** Annotation type declaration */ANNOTATION_TYPE,/** Package declaration */PACKAGE,/*** Type parameter declaration** @since 1.8*/TYPE_PARAMETER,/*** Use of a type** @since 1.8*/TYPE_USE
}
ElementType.java

    坑01:自定义注解允许定义成员,但是这里的成员在进行声明时必须是无入参、无抛出异常的方式进行声明;而且成员只能是方法

    坑02:在给成员指定默认值是必须使用default关键字

package cn.test.demo.base_demo.annotations;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 NeedTest {boolean value() default true;
}

  1.2 使用注解

    1.2.1 创建一个SrpingBoot项目

      下载地址:点击前往

    1.2.2 新建一个自定义注解类

package cn.test.demo.base_demo.annotations;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 NeedTest {boolean value() default true;
}
NeedTest.java

    1.2.3 新建一个服务类

      在该服务类的方法中使用刚刚创建的自定义注解

package cn.test.demo.base_demo.service;import cn.test.demo.base_demo.annotations.NeedTest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;/*** @author 王杨帅* @create 2018-04-29 21:31* @desc 学生服务类**/
@Service
@Slf4j
public class StudentService {private final String className = getClass().getName();@NeedTest()public void insert() {log.info("===/" + className + "/insert===新增操作");}@NeedTest(value = false)public void delete() {log.info("===/" + className + "/delete===删除操作");}}
StudentService.java

    1.2.4 访问注解

      从Java5开始包、类、构造器、方法、字段等都反射对象都开始支持访问注解信息的方法

    /*** {@inheritDoc}* @throws NullPointerException  {@inheritDoc}* @since 1.5*/public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {return super.getAnnotation(annotationClass);}
package cn.test.demo.base_demo.service;import cn.test.demo.base_demo.annotations.NeedTest;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.lang.reflect.Method;import static org.junit.Assert.*;@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class StudentServiceTest {private final String className = getClass().getName();@Testpublic void test01() {Class cla = StudentService.class;Method[] methods = cla.getDeclaredMethods();log.info("===/" + className + "/test01===方法数量为:{}", methods.length);for (Method method : methods) {NeedTest nt = method.getAnnotation(NeedTest.class);if (nt.value()) {log.info("===" + method.getName() + "()需要测试");} else {log.info("===" + method.getName() + "()不需要进行测试");}}}@Testpublic void insert() throws Exception {}@Testpublic void delete() throws Exception {}}
StudentServiceTest.java

 

      

 

转载于:https://www.cnblogs.com/NeverCtrl-C/p/8973014.html

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

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

相关文章

MFC处理回车窗口消失

MFC处理回车窗口消失 2011-04-24 12:21:31| 分类&#xff1a; C&C&VC | 标签&#xff1a;对话框 回车 mfc 消失 |字号大中小 订阅 我的方法是&#xff1a;找到自己设计的按钮&#xff0c;在属性-风格中选择default button&#xff1b;如果没有自己设计的but…

您的框架有多可扩展性?

在参加会议时&#xff0c;我们总是会见到高素质的决策者&#xff0c;他们经常问同样的问题&#xff1a; 您的框架有多可扩展性&#xff1f;如果我需要比您开箱即用的功能更多的东西怎么办&#xff1f; 。 这个问题是非常合理的&#xff0c;因为他们只是不想被卡在开发曲线的中间…

linux常用命令:touch 命令

linux的touch命令不常用&#xff0c;一般在使用make的时候可能会用到&#xff0c;用来修改文件时间戳&#xff0c;或者新建一个不存在的文件。 1&#xff0e;命令格式&#xff1a; touch [选项]... 文件... 2&#xff0e;命令参数&#xff1a; -a 或--timeatime或--timeacces…

用回车键实现MFC对话框中TAB键控件输入焦点在控件中跳转的效果(转)

用回车键实现MFC对话框中TAB键控件输入焦点在控件中跳转的效果&#xff08;转&#xff09; 版权声明&#xff1a;转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://hcq11.blogbus.com/logs/54217707.html 近日在为一个数据应用写数据输入界面&#xff0c;大量…

python-面向对象编程设计与开发

编程范式 1、对不同类型的任务&#xff0c;所采取不同的解决问题的思路。 2、编程范式有两种 1、面向过程编程 2、面向对象编程 面向过程编程 什么是面向过程编程&#xff1f; 过程——解决问题的步骤 要解决一个大的问题 1、先把大问题拆分成若干小问题或子过程。 2、然后子过…

MFC下列表控件的使用

MFC下列表控件的使用 2012-11-09 16:46:57| 分类&#xff1a; 程序VC相关 | 标签&#xff1a; |字号大中小 订阅 1、应该加入头文件 #include <Atlbase.h>2、示例m_list.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_EX_ONECLICKACTIVATE);m_lis…

jbehave_使用JBehave,Gradle和Jenkins的行为驱动开发(BDD)

jbehave行为驱动开发 &#xff08;BDD&#xff09;是一个协作过程 &#xff0c;产品所有者&#xff0c;开发人员和测试人员可以合作交付可为业务带来价值的软件。 BDD是 测试驱动开发 &#xff08;TDD&#xff09; 的合理下一步 。 行为驱动的发展 本质上&#xff0c;BDD是一…

Confluence 6 考虑使用自定义 CSS

CSS 的知识储备 如果你没有有关 CSS 的相关知识&#xff0c;请参考页面 CSS Resources section 中的内容。当你打算开始对 Confluence 的样式表进行修改之前&#xff0c;你应该对 CSS 有一些相关的了解和知识储备。 安全 自定义 CSS 有可能被在页面中注入脚本&#xff0c;有跨…

MFC--CColorDialog的使用

MFC--CColorDialog的使用 2012-05-07 11:05:32| 分类&#xff1a; 学习mfc/c | 标签&#xff1a; |字号大中小 订阅 要在类中定义一个存储颜色的变量COLORREF m_color; 创建一个按钮&#xff0c;用来调用CColorDialog&#xff0c;用以改变静态文本的颜色&#xff0c;&a…

嵌入式 开发——DMA内存到外设

学习目标 加强理解DMA数据传输过程加强掌握DMA的初始化流程掌握DMA数据表查询理解源和目标的配置理解数据传输特点能够动态配置源数据学习内容 需求 串口发送数据 uint8_t data = 0x01; 串口发送(data); 实现串口的发送数据, 要求采用dma的方式 数据交互流程 CPU配置好DM…

使用Java第2部分查询DynamoDB项

在上一篇文章中&#xff0c;我们有机会发布了一些基本的DynamoDB查询操作。 但是&#xff0c;除了基本操作之外&#xff0c;DynamoDB api还为我们提供了一些额外的功能。 投影是具有类似选择功能的功能。 您选择应从DynamoDB项中提取哪些属性。 请记住&#xff0c;使用投影不…

XSS

1.什么是xss XSS攻击全称跨站脚本攻击&#xff0c;是为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆&#xff0c;故将跨站脚本攻击缩写为XSS&#xff0c;XSS是一种在web应用中的计算机安全漏洞&#xff0c;它允许恶意web用户将代码植入到提供给其它用户使用的页面中…

C++中引用传递与指针传递区别(进一步整理)

C中引用传递与指针传递区别&#xff08;进一步整理&#xff09; 博客分类&#xff1a; C/C CCC#J# 从概念上讲。指针从本质上讲就是存放变量地址的一个变量&#xff0c;在逻辑上是独立的&#xff0c;它可以被改变&#xff0c;包括其所指向的地址的改变和其指向的地址中所存放的…

Python之匿名函数

一、匿名函数&#xff1a;也叫lambda表达式 1.匿名函数的核心&#xff1a;一些简单的需要用函数去解决的问题&#xff0c;匿名函数的函数体只有一行 2.参数可以有多个&#xff0c;用逗号隔开 3.返回值和正常的函数一样可以是任意的数据类型 二、匿名函数练习 请把下面的函数转换…

C++中const用法总结

C中const用法总结 Posted on 2009-04-21 22:55 月光林地 阅读(7821) 评论(2) 编辑 收藏 1. const修饰普通变量和指针 const修饰变量&#xff0c;一般有两种写法&#xff1a; const TYPE value; TYPE const value; 这两种写法在本质上是一样的。它的含义是&#xff1a;const修…

vaadin_5分钟内Google App Engine上的Vaadin App

vaadin在本教程中&#xff0c;您将学习如何创建第一个Vaadin Web应用程序&#xff0c;如何在本地AppEngine开发服务器上运行它以及如何将其部署到Google App Engine基础结构。 所有这些大约需要5到10分钟。 是的&#xff0c;如果您已经安装了必要的先决条件&#xff0c;则可以立…

【Java深入研究】10、红黑树

一、红黑树介绍 红黑树是二叉查找树&#xff0c;红黑树的时间复杂度为: O(lgn) 红黑树的特性&#xff1a;&#xff08;1&#xff09;每个节点或者是黑色&#xff0c;或者是红色。&#xff08;2&#xff09;根节点是黑色。&#xff08;3&#xff09;每个叶子节点&#xff08;NIL…

MATLAB排序函数

MATLAB排序函数 (2011-06-29 13:02:08) 源自网络 sort(A)若A是向量不管是列还是行向量&#xff0c;默认都是对A进行升序排列。sort(A)是默认的升序&#xff0c;而sort(A,descend)是降序排序。 sort(A)若A是矩阵&#xff0c;默认对A的各列进行升序排列 sort(A,dim) dim1时等效s…

【分形】【洛谷P1498】

https://www.luogu.org/problemnew/show/P1498 题目描述 自从到了南蛮之地&#xff0c;孔明不仅把孟获收拾的服服帖帖&#xff0c;而且还发现了不少少数民族的智慧&#xff0c;他发现少数民族的图腾往往有着一种分形的效果&#xff0c;在得到了酋长的传授后&#xff0c;孔明掌握…

Java认证:认证或不认证

专业认证始终是一个有争议的主题&#xff0c;有资格的人在争论收益与成本/时间之间的关系。 通过Oracle的Java认证&#xff0c;我认为有两个主要的受众可以从中受益&#xff1a; 那些开始从事软件事业的人。 扎实的工作经验和可证明的代码将永远是潜在雇主的首要考虑因素。 但…