java 自定义注解
java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。 注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。包含在 java.lang.annotation 包中。
1、元注解
元注解是指注解的注解。包括 @Retention @Target @Document @Inherited四种。
1.1、@Retention: 定义注解的保留策略
- @Documented
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.ANNOTATION_TYPE)
- public@interface Target {
- ElementType[] value();
- }
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Target { ElementType[] value(); }
- @Documented
- @Target({ElementType.TYPE,ElementType.METHOD})
- @Retention(RetentionPolicy.RUNTIME)
- public@interface Yts {
- publicenum YtsType{util,entity,service,model}
- public YtsType classType() default YtsType.util;
- }
@Documented @Target({ElementType.TYPE,ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface Yts { public enum YtsType{util,entity,service,model} public YtsType classType() default YtsType.util; }
- @Documented
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- @Inherited
- public@interface HelloWorld {
- public String name()default"";
- }
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Inherited public @interface HelloWorld { public String name()default ""; }
@Retention(RetentionPolicy.RUNTIME)
定义的这个注解是注解会在class字节码文件中存在,在运行时可以通过反射获取到。
@Target({ElementType.TYPE,ElementType.METHOD})
因此这个注解可以是类注解,也可以是方法的注解
这样一个注解就自定义好了,当然注解里面的成员可以为基本的数据类型,也可以为数据,Object等等
3 注解是定义好了,那么怎么来得到,解析注解呢?
java的反射机制可以帮助,得到注解,代码如下:
- publicclass ParseAnnotation {
- publicvoid parseMethod(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException{
- Object obj = clazz.getConstructor(new Class[]{}).newInstance(new Object[]{});
- for(Method method : clazz.getDeclaredMethods()){
- HelloWorld say = method.getAnnotation(HelloWorld.class);
- String name = "";
- if(say != null){
- name = say.name();
- method.invoke(obj, name);
- }
- Yts yts = (Yts)method.getAnnotation(Yts.class);
- if(yts != null){
- if(YtsType.util.equals(yts.classType())){
- System.out.println("this is a util method");
- }else{
- System.out.println("this is a other method");
- }
- }
- }
- }
- @SuppressWarnings("unchecked")
- publicvoid parseType(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
- Yts yts = (Yts) clazz.getAnnotation(Yts.class);
- if(yts != null){
- if(YtsType.util.equals(yts.classType())){
- System.out.println("this is a util class");
- }else{
- System.out.println("this is a other class");
- }
- }
- }
- }
public class ParseAnnotation { public void parseMethod(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException{ Object obj = clazz.getConstructor(new Class[]{}).newInstance(new Object[]{}); for(Method method : clazz.getDeclaredMethods()){ HelloWorld say = method.getAnnotation(HelloWorld.class); String name = ""; if(say != null){ name = say.name(); method.invoke(obj, name); } Yts yts = (Yts)method.getAnnotation(Yts.class); if(yts != null){ if(YtsType.util.equals(yts.classType())){ System.out.println("this is a util method"); }else{ System.out.println("this is a other method"); } } } } @SuppressWarnings("unchecked") public void parseType(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{ Yts yts = (Yts) clazz.getAnnotation(Yts.class); if(yts != null){ if(YtsType.util.equals(yts.classType())){ System.out.println("this is a util class"); }else{ System.out.println("this is a other class"); } } } }
前一个方法是解析得到方法注解的,后一个方法是得到类注解的
以下是测试方法类
- @Yts(classType =YtsType.util)
- publicclass SayHell {
- @HelloWorld(name = " 小明 ")
- @Yts
- publicvoid sayHello(String name){
- if(name == null || name.equals("")){
- System.out.println("hello world!");
- }else{
- System.out.println(name + "say hello world!");
- }
- }
- }
@Yts(classType =YtsType.util) public class SayHell { @HelloWorld(name = " 小明 ") @Yts public void sayHello(String name){ if(name == null || name.equals("")){ System.out.println("hello world!"); }else{ System.out.println(name + "say hello world!"); } } }
- public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException {
- ParseAnnotation parse = new ParseAnnotation();
- parse.parseMethod(SayHell.class);
- parse.parseType(SayHell.class);
- }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)