自定义注解
来源:互联网 发布:java程序设计实验心得 编辑:程序博客网 时间:2024/06/11 21:25
package cn.stu;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface MyController {String controller() default "";}
package cn.stu;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target; @Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface MyService {String service() default "";}
package cn.stu;public class MyActivity { @MyController(controller="login")private String controller;@MyService(service="loginService")private String service;public String getController() {return controller;}public void setController(String controller) {this.controller = controller;}public String getService() {return service;}public void setService(String service) {this.service = service;} }
package cn.stu;import java.lang.reflect.Field;/*** * * 猜测,Spring在初始化的时候, 有一个扫描的配置, 在这里就获取某一个包下面的所有的类。 然后通过反射就对其中的类进行数据的填充。 * 进行初始化扫描下面的类。 (个人猜测) * * * 注解形式主要的也就是通过反射机制来进行数据的填充,在代码运行的时候执行。 如此理解Spring的注解的形式就好理解了。所有的注解也就 * 是一个差不多的普通类而已,只是通过反射来进行初始化和数据的填充。 * * */public class Main {public static void main(String[] args) { String[] str = Main.getFiledInfo(MyActivity.class); for (String string : str) {System.out.println(string);}}// 反射解析注解数据public static String[] getFiledInfo(Class<?> clazz){String[] strData = new String[2];Field[] fileds = clazz.getDeclaredFields(); // 反射获取所有属性,这里只是定义来属性注解for (Field field : fileds) { if(field.isAnnotationPresent(MyController.class)){ MyController controller = (MyController)field.getAnnotation(MyController.class); String loginController = controller.controller(); strData[0] = "登录请求地址->"+loginController; }else if(field.isAnnotationPresent(MyService.class)){ MyService service = (MyService)field.getAnnotation(MyService.class); String loginService = service.service(); strData[1] = "登录业务处理->"+loginService; }}return strData;}}
打印如下:
登录请求地址->login
登录业务处理->loginService
0 0
- 自定义注解
- 自定义注解
- 自定义注解
- 自定义注解
- 自定义注解
- 自定义注解
- 自定义注解
- @自定义注解
- 自定义注解
- 自定义注解
- 自定义注解
- 自定义注解
- 自定义注解
- 自定义注解
- 自定义注解
- 自定义注解
- 自定义注解
- 自定义注解
- 常用命令
- 利用FS寄存器获取KERNEL32.DLL基址算法的证明
- Atitit 贝叶斯算法的原理以及垃圾邮件分类的原理
- Atitti 文本分类 以及 垃圾邮件 判断原理 以及贝叶斯算法的应用解决方案
- Atitit机器学习原理与概论book attilax总结
- 自定义注解
- Atitit 基于meta的orm,提升加速数据库相关应用的开发
- Atian inputmethod 输入法解决方案 方言与多语言多文字支持 英语汉字汉语阿拉伯文的支持 (au
- 多线程同步Mutex
- centOS下安装JDK、Tomcat以及基本配置
- vs2008【断点无效】解决方法
- 怎么在Release下调试代码
- 多线程 临界区CRITICAL_SECTION
- 深入理解Windows X64调试