Java Spring Boot VS .NetCore (一)来一个简单的 Hello World

Java Spring Boot VS .NetCore (二)实现一个过滤器Filter

Java Spring Boot VS .NetCore (三)Ioc容器处理

Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore

Java Spring Boot VS .NetCore (五)MyBatis vs EFCore

Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml

Java Spring Boot VS .NetCore (七) 配置文件

Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute

Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security

Java Spring Boot VS .NetCore (十) Java Interceptor vs .NetCore Interceptor

Java Spring Boot VS .NetCore (十一)自定义标签 Java Tag Freemarker VS .NetCore Tag TagHelper

继续前面的章节,这里我介绍下注解,其实Java注解跟.NetCore的特性标签类似,下面我们通过代码来说明

Java自定义注解

首先我先说下Java注解需要使用的注解

  1. @Target(value = ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented

Taget:指定注解在什么地方生效,作用于什么对象上,参数很多这里把源码拉出来了,每一个的意思就不过多介绍,一看就明白

  1. public enum ElementType {
  2. /** 类、接口(包括注释类型)或枚举声明 */
  3. TYPE,
  4.  
  5. /** 字段声明(包括枚举常量) */
  6. FIELD,
  7.  
  8. /** 方法声明 */
  9. METHOD,
  10.  
  11. /** 形式参数声明 */
  12. PARAMETER,
  13.  
  14. /** 构造函数声明 */
  15. CONSTRUCTOR,
  16.  
  17. /** 局部变量声明 */
  18. LOCAL_VARIABLE,
  19.  
  20. /** 注释类型声明 */
  21. ANNOTATION_TYPE,
  22.  
  23. /** 程序包声明 */
  24. PACKAGE,
  25.  
  26. /**
  27. * 类型参数声明
  28. *
  29. * @since 1.8
  30. */
  31. TYPE_PARAMETER,
  32.  
  33. /**
  34. * 使用类型
  35. *
  36. * @since 1.8
  37. */
  38. TYPE_USE
  39. }
  1. public enum RetentionPolicy {
  2. /**
  3. 注解将被编译器丢弃
  4. */
  5. SOURCE,
  6.  
  7. /**
  8. 注解将由编译器记录在类文件中,但在运行时不需要由VM保留。这是默认的行为。
  9. */
  10. CLASS,
  11.  
  12. /**
  13. 注解将由编译器记录在类文件中,并在运行时由VM保留,因此可以通过反射读取当前注解。*/
  14. RUNTIME
  15. }
  1. @Documented
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target(ElementType.ANNOTATION_TYPE)
  4. public @interface Documented {
  5. /*
  6. 注解表明这个注解应该被 javadoc工具记录
  7. */
  8. }

下面就来模拟一个自定义的注解,同时简单并模拟MyBatis中的像如下写法,解析下面代码的实现原理

  1. @Select("SELECT username,email,newname,nick_name FROM user_model")
  2. @Results({
  3. @Result(property = "username", column = "username"),
  4. @Result(property = "email", column = "email"),
  5. @Result(property = "newname", column = "newname"),
  6. @Result(property = "nickName", column = "nick_name")
  7. })
  8. List<UserModel> getAll();

这里定义一个自定义的注解接口 代码如下,注解要作用于方法上

  1. Target(value = ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface CustomSelect {
  5. String Sql() default "";
  6. }

这一步也非常简单,定义一个操作接口,使用自定义的注解,添加好相关的Sql语句

  1. public interface ReflectorDao {
  2.  
  3. @CustomSelect(Sql = "select * from user_model where id=1")
  4. int InsertModel();
  5. }

接下来就是怎么使用了,当然这里还是要用到反射 ,这里我添加了一个测试方法,里面模拟实现了一个JDBC的操作方法

  1. @Test
  2. public void testReflectorDao() throws Exception {
  3. Class<?> c= ReflectorDao.class;
  4. Method method=c.getMethod("InsertModel");
  5. CustomSelect customSelect=method.getAnnotation(CustomSelect.class);
  6. String strsql= customSelect.Sql();
  7. System.out.print(strsql+"\r\n");
  8. //调用JDBC完成操作
  9. JDBCHelper.ExcuteQuery(strsql);
  10.  
  11. }

这里反射里面的方法就不特殊说明了,这里说下 获取注解的方法把,如下

  1. // // 获取某个类型的注解
  2. // public <A extends Annotation> A getAnnotation(Class<A> annotationClass);
  3. // // 获取所有注解(包括父类中被Inherited修饰的注解)
  4. // public Annotation[] getAnnotations();
  5. // // 获取声明的注解(但是不包括父类中被Inherited修饰的注解)
  6. // public Annotation[] getDeclaredAnnotations();
  7. // // 判断某个对象上是否被某个注解进行标注
  8. // public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
  9. //
  10. // // 获取某个类声明的所有字段
  11. // public Field[] getDeclaredFields() throws SecurityException;
  12. // // 获取某个方法
  13. // public Method getMethod(String name, Class<?>... parameterTypes);

执行下单元测试: OK

.NetCore Attribute

Java中有的 .NetCore一样能实现,这里就要介绍.NetCore总的特性了,就是Attribute类,怎么来使用这个呢?不要急,通过带来是解释,自定义的特性需要继承Attribute类,且类名可以以Attribute结尾,这样在使用的时候就可以通过前面的名称来写,代码如下

  1. [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
  2. public class CustomAttribute : Attribute
  3. {
  4. public string Sql { get; set; }
  5.  
  6. }

看到这里,就有一种熟悉感,看下源码,这里什么Class 等等更Java用法一样标识该特性作用于什么对象上,AllowMultiple :标识是否可以指定多次,同一个对象上多次使用特性,Inherited:当前特性是否可以被继承

  1. //
  2. // 摘要:
  3. // Specifies the application elements on which it is valid to apply an attribute.
  4. [Flags]
  5. public enum AttributeTargets
  6. {
  7. //
  8. // 摘要:
  9. // Attribute can be applied to an assembly.
  10. Assembly = ,
  11. //
  12. // 摘要:
  13. // Attribute can be applied to a module.
  14. Module = ,
  15. //
  16. // 摘要:
  17. // Attribute can be applied to a class.
  18. Class = ,
  19. //
  20. // 摘要:
  21. // Attribute can be applied to a structure; that is, a value type.
  22. Struct = ,
  23. //
  24. // 摘要:
  25. // Attribute can be applied to an enumeration.
  26. Enum = ,
  27. //
  28. // 摘要:
  29. // Attribute can be applied to a constructor.
  30. Constructor = ,
  31. //
  32. // 摘要:
  33. // Attribute can be applied to a method.
  34. Method = ,
  35. //
  36. // 摘要:
  37. // Attribute can be applied to a property.
  38. Property = ,
  39. //
  40. // 摘要:
  41. // Attribute can be applied to a field.
  42. Field = ,
  43. //
  44. // 摘要:
  45. // Attribute can be applied to an event.
  46. Event = ,
  47. //
  48. // 摘要:
  49. // Attribute can be applied to an interface.
  50. Interface = ,
  51. //
  52. // 摘要:
  53. // Attribute can be applied to a parameter.
  54. Parameter = ,
  55. //
  56. // 摘要:
  57. // Attribute can be applied to a delegate.
  58. Delegate = ,
  59. //
  60. // 摘要:
  61. // Attribute can be applied to a return value.
  62. ReturnValue = ,
  63. //
  64. // 摘要:
  65. // Attribute can be applied to a generic parameter.
  66. GenericParameter = ,
  67. //
  68. // 摘要:
  69. // Attribute can be applied to any application element.
  70. All =
  71. }

下面我们同样用接口来实现,代码如下,前面说了 自定义属性已Attribute结尾的类名,写的时候直接写Custom

  1. public interface RelactorDao
  2. {
  3.  
  4. [Custom(Sql = "select * from user_model where id=1")]
  5. void InsertModel();
  6.  
  7. }

这里我写一个测试类是看下,由于时间的关系,这里就不写SqlHelper 来执行了,输入下Sql就行了,这个跟Java一样需要使用反射,思想一样,只是使用方法名称不同而已,具体的方法就不做介绍..有兴趣自己了解下

  1. public class TestClass
  2. {
  3. public void TestMethod()
  4. {
  5. var type = typeof(RelactorDao);
  6. MethodInfo methodInfo= type.GetMethod("InsertModel");
  7. var atrrs = methodInfo.GetCustomAttributes(typeof(CustomAttribute), false) as CustomAttribute[];
  8. var strSql = atrrs.First().Sql;
  9. //当然这里也可以执行
  10. Console.WriteLine(strSql);
  11. }
  12. }

接下来我们在看下执行效果 OK

总结

这里只是简单的模拟下,其实要实现MyBatis中的注解的功能,其实还需要其他的知识,面向切面编程的技术AOP

Java中的 @Aspect 注解 ,.NetCore 可以通过动态代理来实现,但是反过来想下,.NetCore中一样可以实现类似于MyBait一样使用方式的ORM框架,可能.NetCore中考虑到大量应用反射会导致性能问题

Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute的更多相关文章

  1. Java Spring Boot VS .NetCore (一)来一个简单的 Hello World

    系列文章 Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filte ...

  2. Java Spring Boot VS .NetCore (二)实现一个过滤器Filter

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  3. Java Spring Boot VS .NetCore (三)Ioc容器处理

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  4. Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  5. Java Spring Boot VS .NetCore (五)MyBatis vs EFCore

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  6. Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  7. Java Spring Boot VS .NetCore (七) 配置文件

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  8. Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  9. Java Spring Boot VS .NetCore (十) Java Interceptor vs .NetCore Interceptor

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

随机推荐

  1. springboot +thymeleaf+myql 记录

    thymeleaf官方文档: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.pdf demo案例:https://github. ...

  2. 作业二:分布式版本控制系统Git的安装与使用

    作业要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 1.下载安装配置用户名和邮箱. (1)下载安装Github配置 ...

  3. Django之缓存、信号和图片验证码

    一. 缓存 1. 介绍 缓存通俗来说:就是把数据先保存在某个地方,下次再读取的时候不用再去原位置读取,让访问速度更快. 缓存机制图解 2.Django中提供了6种缓存方式 1. 开发调试 2. 内存 ...

  4. [NOIp2016] 换教室

    题目类型:期望\(DP\) 传送门:>Here< 题意:现有\(N\)个时间段,每个时间段上一节课.如果不申请换教室,那么时间段\(i\)必须去教室\(c[i]\)上课,如果申请换课成功, ...

  5. 【并发编程】【JDK源码】JDK的(J.U.C)java.util.concurrent包结构

    本文从JDK源码包中截取出concurrent包的所有类,对该包整体结构进行一个概述. 在JDK1.5之前,Java中要进行并发编程时,通常需要由程序员独立完成代码实现.当然也有一些开源的框架提供了这 ...

  6. Luogu P4643 【模板】动态dp

    题目链接 Luogu P4643 题解 猫锟在WC2018讲的黑科技--动态DP,就是一个画风正常的DP问题再加上一个动态修改操作,就像这道题一样.(这道题也是PPT中的例题) 动态DP的一个套路是把 ...

  7. python selenium简单安装及使用

    1.安装 pip install selenium 2.下载浏览器对应的Driver 链接地址:https://sites.google.com/a/chromium.org/chromedriver ...

  8. [报错]java.lang.ClassCastException

    Caused by: java.lang.ClassCastException: org.apache.xml.dtm.ref.DTMManagerDefault cannot be cast to ...

  9. java集合框架综述

    一.集合框架图 简化图: 说明:对于以上的框架图有如下几点说明 1.所有集合类都位于java.util包下.Java的集合类主要由两个接口派生而出:Collection和Map,Collection和 ...

  10. 面试:atoi() 与 itoa()函数的内部实现(转)

    原 面试:atoi() 与 itoa()函数的内部实现 2013年04月19日 12:05:56 王世晖 阅读数:918   #include <stdio.h> #include < ...