什么是注解这些东西请看 think in java
在这个实例中演示的是注解的创建和利用反射原理去获取代码信息
1、解释本实例用到的注解及解释
2、创建注解
3、创建使用注解的类
4、利用反射机制去解析类代码信息
============================================================================================
1、注解的方法不能有参数
注解的返回类型是 “基本类型” “字符串” “枚举” “Class” “返回类型的数组”
注解可以添加元注解:
Documented:将此注解包在javadoc中
Target:表示该注解用在什么地方,method:声明方法
Inherited:表示继承父类中的注解
Retention:表示在什么级别保存该注解信息,RetentionPolicy.RUNTIME:运行期 间保留
2、
- package com.gyj.annotation;
- import java.lang.annotation.Documented;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Inherited;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- /**
- * 创建注解
- * @author GYJ
- * @date 2014-3-23
- */
- @Documented
- @Target(ElementType.METHOD)
- @Inherited
- @Retention(RetentionPolicy.RUNTIME)
- public @interface MethodInfo {
- String author() default "gyj";
- String date();
- int revision() default 1;
- String comments();
- }
3、
- package com.gyj.annotation;
- import java.util.ArrayList;
- import java.util.List;
- public class AnnotationExample {
- /**
- * @param args
- */
- public static void main(String[] args) {
- }
- @Override
- @MethodInfo(author = "gyj", comments = "main method", date = "2014", revision = 1)
- public String toString() {
- return "overriden to string method";
- }
- @Deprecated
- @MethodInfo(comments = "method1", date = "2019")
- public static void oldMethod() {
- System.out.println("old method don't use it");
- }
- @SuppressWarnings({"unchecked", "deprecation"})
- @MethodInfo(author = "gyj", comments = "main genericsTest", date = "2015", revision = 2)
- public static void genericsTest() {
- List l = new ArrayList<>();
- l.add("abc");
- //发出警告
- oldMethod();
- }
- }
4、
- package com.gyj.annotation;
- import java.lang.annotation.Annotation;
- import java.lang.reflect.Method;
- public class AnnotationParsing {
- /**
- * 解析注解
- * @param args
- */
- public static void main(String[] args) {
- try {
- Method[] method = Class.forName("com.gyj.annotation.AnnotationExample").getMethods();
- for (Method method1 : method) {
- //验证MethodInfo注解是否可以继承
- if (method1.isAnnotationPresent(com.gyj.annotation.MethodInfo.class)) {
- for (Annotation annotation : method1.getAnnotations()) {
- //具体的注解信息和方法
- System.out.println("注解" + annotation + "存在方法" + method1 + "\n");
- }
- //根据注解信息条件性的做出操作
- MethodInfo methodInfo = method1.getAnnotation(MethodInfo.class);
- if (methodInfo.revision() == 1) {
- System.out.println("revision 是 1 的方法是 " + methodInfo);
- }
- }
- }
- } catch (SecurityException | ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- //out put===========================
- /*注解@com.gyj.annotation.MethodInfo(author=gyj, revision=1, comments=main method, date=2014)存在方法public java.lang.String com.gyj.annotation.AnnotationExample.toString()
- revision 是 1 的方法是 @com.gyj.annotation.MethodInfo(author=gyj, revision=1, comments=main method, date=2014)
- 注解@java.lang.Deprecated()存在方法public static void com.gyj.annotation.AnnotationExample.oldMethod()
- 注解@com.gyj.annotation.MethodInfo(author=gyj, revision=1, comments=method1, date=2019)存在方法public static void com.gyj.annotation.AnnotationExample.oldMethod()
- revision 是 1 的方法是 @com.gyj.annotation.MethodInfo(author=gyj, revision=1, comments=method1, date=2019)
- 注解@com.gyj.annotation.MethodInfo(author=gyj, revision=2, comments=main genericsTest, date=2015)存在方法public static void com.gyj.annotation.AnnotationExample.genericsTest()*/
- }
相关推荐
自定义注解得使用,模拟spring通过注解方式创建bean实例!
Spring注解驱动开发第41讲——Spring IOC容器创建源码解析(一)之BeanFactory的创建以及预准备工作(合起来整个过程)
我们打开配置件,API Platform会动的检索Entity录中的所有类,读取类前的注解,然后根据注解的设置成回到浏览器,我们刷新Api档,API Platf
Spring Boot是一个开源框架,旨在简化Spring应用程序的创建和部署。它通过提供自动配置和约定优于配置的...通过了解这些常用注解的特性和使用方法,读者可以更有效地利用Spring Boot框架的功能,提高开发效率和质量。
Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点
创建xml和注解方式开发springmvc
它可以用于创建文档,跟踪代码中的依赖性,甚至执行基本编译时检查。注解是以‘@注解名’在代码中存在的,根据注解参数的个数,我们可以将注解分为:标记注解、单值注解、完整注解三类。它们都不会直接影响到程序的...
Visual Studio开发软件-win32项目创建简单窗口详细代码
本项目提供一个简单索引构建工具,使用注解即可实现索引自动创建与搜索,而不需要你手写该过程, 避免项目中大量使用like或者其他效率低的搜索机制,相比于ES(专业搜索引擎),这是一种轻量级的实现方式 特点: ...
注解+自定义注解+元注解+理解 - 本资源是一个注解的理解,解释了注解的作用和分类,以及如何用Java创建和使用自定义注解,包括使用元注解,反射,处理器等。
说明创建注解我在第一个注解说明下,以后的注解不在说明。‘ DBTable 注解: /** * Project Name:myannotation * File Name:DBTable.java * Package Name:com.iflytek.db * Date:2016-8-28下午08:20:54 * ...
@ConfigurationProperties注解使用方法(源代码) 目录 @ConfigurationProperties注解使用方法 前言 一、Spring配置方式 1.1 第一阶段:xml配置 1.2 第二阶段:注解配置 1.3 第三阶段:Java配置(java config) 二、@...
注解三表ssh框架
注解Annotation实现原理与自定义注解例子 每当你创建描述符性质的类或者接口时,一旦其中包含重复性的工 ...作,就可以考虑使用注解来简化与自动化该过程。 Java提供了四种元注解,专门负责新注解的创建工作
JPA注解创建数据库表 和hibernate.cfg.xml 建表
JetCache是一个基于java的缓存系统封装,提供统一的API和注解简化缓存的使用。...通过注解创建并配置Cache实例 针对所有Cache实例和方法缓存的自动统计 Key的生成策略和value的序列化策略是可以配置的
主要介绍了Spring如何使用注解的方式创建bean,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring注解驱动开发的源码,包括Spring常用的注解,工具使用idea创建的maven项目
用来限制被修饰注解的使用范围,即注解可以在类的哪些成员上使用 @Target 取值使用ElementType.() 1. CONSTRUCTOR:可以在构造器上使用注解 2. FIELD:可以在字段上使用注解 ...
在sqlite 中,利用注解,反射,将jave bean转为建表语句。利用反射将cursor构建bean或者list.