注解_Annotation_内置注解_自定义注解_反射机制读取注解JAVA208-210

2024-09-03 14:38

本文主要是介绍注解_Annotation_内置注解_自定义注解_反射机制读取注解JAVA208-210,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

来源:http://www.bjsxt.com/
一、S02E208_01注解_Annotation、内置注解

什么是注解
什么是注解

内置注解
内置注解
SuppressWarnings

二、S02E209_01自定义注解

自定义注解
自定义注解1

元注解
元注解

元注解1
元注解2

package com.test.annotation;@MyAnnotation01
public class Demo01 {@MyAnnotation01(age=19,studentName="test",id=1001,schools={"北大","浙大"})public static void main(String[] args) {}@MyAnnotation02(value="aaa")//或者("aaa")public void test(){}
}
package com.test.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(value={ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation01 {String studentName() default "";int age() default 0;int id() default -1;String[] schools() default{"清华","复旦"};
}
package com.test.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(value={ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation02 {String value();
}

三、S02E210_01反射机制读取注解

注解作业
注解作业

package com.test.annotation2;import java.lang.annotation.Annotation;
import java.lang.reflect.Field;/*** 使用反射读取注解的信息,模拟处理注解信息的流程*/
public class Demo {public static void main(String[] args) {try {Class clazz = Class.forName("com.test.annotation2.SxtStudent");//获取类的所有有效注解Annotation[] annotations = clazz.getAnnotations();for (Annotation a : annotations) {System.out.println(a);}//获取类的指定的注解SxtTable st = (SxtTable) clazz.getAnnotation(SxtTable.class);System.out.println(st.value());//获取类的属性的注解Field f = clazz.getDeclaredField("sname");SxtField sxtField = f.getAnnotation(SxtField.class);System.out.println(sxtField.columnName()+"--"+sxtField.type()+"--"+sxtField.length());//根据获取的表名和字段的信息,拼出DDL语句,然后,使用JDBC执行这个SQL,在数据库中生成相关的表} catch (Exception e) {e.printStackTrace();}}
}
package com.test.annotation2;@SxtTable("tb_student")
public class SxtStudent {@SxtField(columnName="id",type="int",length=10)private int id;@SxtField(columnName="sname",type="varchar",length=10)private String sname;@SxtField(columnName="age",type="int",length=3)private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getStudentName() {return sname;}public void setStudentName(String studentName) {this.sname = studentName;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
package com.test.annotation2;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(value={ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtTable {String value();
}
package com.test.annotation2;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(value={ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtField {String columnName();String type();int length();
}

控制台输出

@com.test.annotation2.SxtTable(value=tb_student)
tb_student
sname--varchar--10

这篇关于注解_Annotation_内置注解_自定义注解_反射机制读取注解JAVA208-210的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1133242

相关文章

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为