当前位置: 首页 > 编程日记 > 正文

java中的注解(二)

今天我继续来介绍java中的注解。注解与接口和类不同的是注解是不允许继承的,但是注解中有一个和继承有关的元注解:@Inherited。如果我们在定义注解时候加上这个元注解那么我们就可以在子类中监测到该注解的存在。

@Target(ElementType.TYPE)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface Action {boolean isSingle() default true;String value() default "";String url() default "";
}@Action
class BaseController{    
}public class UserController extends BaseController{public static void main(String[] args) {System.out.println(UserController.class.isAnnotationPresent(Action.class)); //trueSystem.out.println(BaseController.class.isAnnotationPresent(Action.class)); //true} 
}

如上代码所示:我在BaseController上加了Action的注解但是没有在UserController上加但是我却可以在UserController类上检查到Action注解的存在就是因为我们在定义注解时加了@Inherited。

我们现在已经可以自己创建注解,接下来我来给大家介绍如何获取注解的信息。在java中是通过反射来获取注解的信息的,在Class,Method,Field,Constructor中都有如下的这几个方法:getAnnotations()获取所有的注解,getDeclaredAnnotations获取所有声明的注解,忽略inherited来的注解,getAnnotations(Class<A> annotationclass)获取指定类型的注解,isAnnotationPresent(Class<A> annotationclass)判断是否有指定类型的注解。下面我来通过代码来获取类上的注解:

@Target(ElementType.TYPE)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@interface Action {boolean isSingle() default true;String value() default "";String url() default "";
}@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Service {String value() default "";String url() default "";
}@Action(value="action",url="actionurl")
@Service(value="service",url="serviceurl")
class BaseController{    
}public class UserController extends BaseController{public static void main(String[] args) throws ClassNotFoundException {Class<BaseController> cls = BaseController.class;Annotation[] annotations = cls.getAnnotations();for (Annotation annotation : annotations) {if(annotation instanceof Action){Action action = (Action)annotation;System.out.println(action.url());    //actionurlSystem.out.println(action.value());  //action}if(annotation instanceof Service){Service service = (Service)annotation;System.out.println(service.url());   //serviceurlSystem.out.println(service.value());  //service}}} 
}

在java中Method和Contructor都有方法参数,而参数也是可以有注解的,所以有一个方法返回参数的注解:getParameterAnnotations()返回值是一个二维数组。我们来看一个例子:

public class TestMethodAnnotations {@Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)static @interface requestParam {String value();}@Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)static @interface DefaultValue {String value() default "";}public void test(@requestParam("useranme") String username,@requestParam("password") @DefaultValue("1234") String password){}public static void main(String[] args) throws NoSuchMethodException, SecurityException {Class<TestMethodAnnotations> cls = TestMethodAnnotations.class;Method method = cls.getMethod("test",new Class[]{String.class,String.class});Annotation[][] anns =  method.getParameterAnnotations();for (Annotation[] annotations : anns) {for (Annotation annotation : annotations) {if(annotation instanceof requestParam){requestParam request = (requestParam)annotation;System.out.println(request.value());}if(annotation instanceof DefaultValue){DefaultValue defaultValue = (DefaultValue)annotation;System.out.println(defaultValue.value());}}}}  
}

代码还是比较的简单的,在这个例子中我们使用@requestParam和@DefaultValue两个注解来修饰方法的参数然后使用getParameterAnnotations这个方法来获取方法参数上的注解。

通过注解我们可以实现很多的功能,比如依赖注入,绑定参数等等,掌握注解的使用有利于我们日常的开发并且为日后学习框架打下基础。

转载于:https://www.cnblogs.com/suyang-java/p/10886092.html

相关文章:

【C++】random随机数与【C++11】/rand()和srand()的用法

文章目录随机数1. c 11 random随机数的使用&#xff08;推荐使用&#xff09;1.11.21.31.42.1 C中随机函数rand()和srand()的用法&#xff08;老本版&#xff09;2.2 限制随机数的范围随机数 C 提供了一组函数以生成和使用随机数字。随机数字就是从一组可能的值中进行随机选择…

快速区域积分直方图实现

void cacHOGinCell(Mat& HOGCellMat,Rect roi,std::vector<Mat>& integrals) {// 实现快速积分HOGint x0 roi.x,y0 roi.y;int x1 x0 roi.width&#xff1b;int y1 y0 roi.height;for(int i 0; i < NBINS; i){// 根据矩阵的上下左右坐标计算Mat integra…

Resultset获取行数和列数

为什么80%的码农都做不了架构师&#xff1f;>>> 在Java中&#xff0c;获得ResultSet的总行数的方法有以下几种。 第一种&#xff1a;利用ResultSet的getRow方法来获得ResultSet的总行数 Statement stmt con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,Re…

object.create()

语法&#xff1a; Object.create(proto, [propertiesObject]) //方法创建一个新对象&#xff0c;使用现有的对象来提供新创建的对象的proto。 参数&#xff1a;proto : 必须。表示新建对象的原型对象&#xff0c;即该参数会被赋值到目标对象(即新对象&#xff0c;或说是最后返回…

codecheck_use_record

文章目录Step 1: Integrate CodeChecker into your build systemStep 2: Analyze your codeStep 3: Run the analysisStep 4: View the analysis results in the command lineStep 5: Hint: You can do the 1st and the 2nd step in one round by executing checkstep 6: Expor…

centos6.5 rsync+inotify同步配置笔记

以两台服务器为例&#xff1a; 主服务器&#xff1a; 192.168.1.100 从服务器&#xff1a; 192.168.1.101 1.安装rsync (主服务器与从服务器同时安装) 使用xinetd管理rsync yum install rsync xinetd 设置开机启动 vi /etc/xinetd.d/rsync ... 修改为 disable no ... 启动xin…

HOG 特征计算实现

// 获取HOG直方图 cv::Mat getHog(Point pt,std::vector<Mat> &integrals) {// 判断当前点的位置是否符合条件 if( pt.x - R < 0 ||pt.y - R < 0 ||pt.x R > integrals[0].cols ||pt.y R > integrals[0].rows ){return Mat();}// 直方图Mat hist(Size(…

Spring+SpringMVC+Mybatis整合

一、简单测试工程搭建1、Mybatis所需要的的jar包&#xff08;包含数据库驱动包和相关的日志包&#xff09;、SpringMVC和Spring的jar包2、然后构建一个基本的工程&#xff0c;这里我们使用mapper代理的方式进行Mybatis的编写&#xff0c;关于mapper代理请参考Mybatis简单入门中…

【Tools】Markdown数学符号公式(史上最全公式表)

Markdown数学符号&公式 文章目录Markdown数学符号&公式1. 希腊字母表2. 希腊字母3. 数学符号表4. 数学符号5. 数学符号补充表6. 数学符号补充1. 希腊字母表 符号代码符号代码α\alphaα\alphaA\AlphaA\Alphaβ\betaβ\betaB\BetaB\Betaγ\gammaγ\gammaΓ\GammaΓ\gam…

Editplus下载、安装并最佳配色方案(强烈推荐)

不多说&#xff0c;直接上干货&#xff01; Editplus下载 第一步&#xff1a;进入官网 https://www.editplus.com/ 第二步&#xff1a;下载 https://www.editplus.com/download.html Editplus安装 我这里&#xff0c;直接以一个压缩包来安装&#xff0c;需要的&#xff0c;请…

MySQL数据库开发规范-EC

最近一段时间一边在线上抓取SQL来优化&#xff0c;一边在整理这个开发规范&#xff0c;尽量减少新的问题SQL进入生产库。今天也是对公司的开发做了一次培训&#xff0c;PPT就不放上来了&#xff0c;里面有十来个生产SQL的案例。因为规范大部分还是具有通用性&#xff0c;所以也…

操作系统与内存管理

操作系统内存管理 文章目录操作系统内存管理1. 虚拟地址空间2. 内存地址空间含义及分配3. 虚拟内存诞生的前世与今生&#xff1f;3.1 内存管理的好处3.2 **内存管理实现总体策略**4. 不同进程如何划分内存地址空间&#xff1f;5 内存分配与回收5.1 buffer和cache5.2 malloc背后…

圆形 LBP 特征

template <typename _Tp> staticinline void elbp_(InputArray _src, OutputArray _dst,int radius, int neighbors) {// 得到数据矩阵Mat src _src.getMat();// 输出矩阵_dst.create(src.rows-2*radius, src.cols-2*radius, CV_32SC1);Mat dst _dst.getMat();// 初始化…

40个Java多线程问题总结

&#xff08;转&#xff09; 这篇文章作者写的真是不错 40个问题汇总 1、多线程有什么用&#xff1f; 一个可能在很多人看来很扯淡的一个问题&#xff1a;我会用多线程就好了&#xff0c;还管它有什么用&#xff1f;在我看来&#xff0c;这个回答更扯淡。所谓"知其然知其所…

SLAM十四讲笔记1

文章目录ch02 初识SLAMch02-01 经典视觉SLAM框架ch02-02 SLAM问题的数学表述ch03 三维空间刚体运动ch03.01 旋转矩阵&#xff1a;点和向量,坐标系01 向量a在线性空间的基[e1,e2,e3][e_1,e_2,e_3][e1​,e2​,e3​]下的坐标为[a1,a2,a3]T[a_1,a_2,a_3]^T[a1​,a2​,a3​]T.02 向量…

12、OpenCV实现图像的空间滤波——图像平滑

1、空间滤波基础概念 1、空间滤波基础 空间滤波一词中滤波取自数字信号处理&#xff0c;指接受或拒绝一定的频率成分&#xff0c;但是空间滤波学习内容实际上和通过傅里叶变换实现的频域的滤波是等效的&#xff0c;故而也称为滤波。空间滤波主要直接基于领域&#xff08;空间域…

计算 LBP 特征

#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/features2d/features2d.hpp> #include <opencv2/features2d/features2d.hpp> // 计算原始LBP特征 cv::Mat OLBP(cv::Mat& srcImage) {const int nRows …

三维重建【四】-------------------结构光 三维重建----论文调研

1. 动态目标实时三维重建-结构光方案 动态目标 三维重建 Stripe boundary codes for real-time structured-light range scanning of moving objects 我们提出了一种新的实时结构光扫描方法。在分析现有结构光技术的基本假设之后&#xff0c;我们基于编码投影条纹之间的边界&…

APP开发定制

app是什么意思? APP&#xff0c;application的简称&#xff0c;是智能手机的第三方应用程序&#xff0c;常见的有微信、手机qq、今日头条、手机支付宝、腾讯视频、微店等&#xff0c;随着智能手机和ipad等移动终端设备的普及&#xff0c;人们逐渐习惯了使用APP客户端上网的方式…

Haar 特征提取

double HaarExtract(double const ** image ,int type_, cv::Rect roi) {double value;double wh1, wh2;double bk1, bk2;int x roi.x;int y roi.y;int width roi.width;int height roi.height;switch (type_){// Haar水平边缘case 0: // HaarHEdgewh1 calcIntegral(image…

awk的基本⽤法

awk的基本⽤法 awk是报告⽣成器&#xff0c;格式化⽂本输出&#xff0c;有多种版本。centos中的是gawk即GNU awk版本。 awk⼯作原理&#xff1a;第⼀步&#xff1a;执⾏BEGIN{action;...}语句块中的语句。第⼆步&#xff1a;从⽂件或标准输⼊&#xff08;stdin&#xff09;读取…

视音频数据处理入门:RGB、YUV像素数据处理【转】

转自&#xff1a;http://blog.csdn.net/leixiaohua1020/article/details/50534150 视音频数据处理入门系列文章&#xff1a; 视音频数据处理入门&#xff1a;RGB、YUV像素数据处理 视音频数据处理入门&#xff1a;PCM音频采样数据处理 视音频数据处理入门&#xff1a;H.264视频…

SVO(SVO: fast semi-direct monocular visual odometry)

SVO&#xff08;SVO: fast semi-direct monocular visual odometry&#xff09;翻译 文章目录SVO&#xff08;SVO: fast semi-direct monocular visual odometry&#xff09;翻译1、介绍2、系统概述3、符号4、运动估计4.1、 基于稀疏模型的图像对齐4.2、 通过特征对齐松弛4.3、…

MSER 候选车牌区域检测

#include "opencv2/highgui/highgui.hpp" #include "opencv2/features2d/features2d.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> // Mser车牌目标检测 std::vector<cv::Rect> mserGetPlate(cv::Mat srcImage…

从HelloWorld看Knative Serving代码实现

为什么80%的码农都做不了架构师&#xff1f;>>> 摘要&#xff1a; Knative Serving以Kubernetes和Istio为基础&#xff0c;支持无服务器应用程序和函数的部署并提供服务。我们从部署一个HelloWorld示例入手来分析Knative Serving的代码细节。 概念先知 官方给出的这…

svo_note

SVO论文笔记1.frame overviews2. Motion Estimate Thread2.1 Sparse Model-based Image Alignment 基于稀疏点亮度的位姿预估2.2 Relaxation Through Feature Alignment 基于图块的特征点匹配2.3 Pose and Structure Refinement3 Mapping Thread3.1 depth-filter3.2 初始化参考…

Druid 配置 wallfilter

这个文档提供基于Spring的各种配置方式 使用缺省配置的WallFilter <bean id"dataSource" class"com.alibaba.druid.pool.DruidDataSource" init-method"init" destroy-method"close">...<property name"filters" v…

vue下的bootstrap table + jquery treegrid, treegrid无法渲染的问题

在mian.js导入的包如下&#xff1a;该bootstrap-table-treegrid.js需要去下载&#xff0c;在复制到jquery-treegrid/js/ 1 import $ from jquery 2 import bootstrap/dist/css/bootstrap.min.css 3 import bootstrap/dist/js/bootstrap.min 4 import bootstrap-table/dist/boot…

内存和缓存的区别

今天看书的时候又看到了内存和缓存&#xff0c;之所以说又&#xff0c;是因为之前遇到过查过资料&#xff0c;但是现在又忘了(图侵删)。 所以又复习一遍&#xff0c;记录一下&#xff0c;有所纰漏的地方&#xff0c;欢迎指正。 同志们&#xff0c;上图并不是内存和缓存中的任何…

【Boost】noncopyable:不可拷贝

【CSDN】&#xff1a;boost::noncopyable解析 【Effective C】&#xff1a;条款06_若不想使用编译器自动生成地函数&#xff0c;就该明确拒绝 1.example boost::noncopyable 为什么要boost::noncopyable 在c中定义一个类的时候&#xff0c;如果不明确定义拷贝构造函数和拷贝赋…