Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute
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注解需要使用的注解
@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
Taget:指定注解在什么地方生效,作用于什么对象上,参数很多这里把源码拉出来了,每一个的意思就不过多介绍,一看就明白
public enum ElementType {
/** 类、接口(包括注释类型)或枚举声明 */
TYPE,
/** 字段声明(包括枚举常量) */
FIELD,
/** 方法声明 */
METHOD,
/** 形式参数声明 */
PARAMETER,
/** 构造函数声明 */
CONSTRUCTOR,
/** 局部变量声明 */
LOCAL_VARIABLE,
/** 注释类型声明 */
ANNOTATION_TYPE,
/** 程序包声明 */
PACKAGE,
/**
* 类型参数声明
*
* @since 1.8
*/
TYPE_PARAMETER,
/**
* 使用类型
*
* @since 1.8
*/
TYPE_USE
}
public enum RetentionPolicy {
/**
注解将被编译器丢弃
*/
SOURCE,
/**
注解将由编译器记录在类文件中,但在运行时不需要由VM保留。这是默认的行为。
*/
CLASS,
/**
注解将由编译器记录在类文件中,并在运行时由VM保留,因此可以通过反射读取当前注解。*/
RUNTIME
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
/*
注解表明这个注解应该被 javadoc工具记录
*/
}
下面就来模拟一个自定义的注解,同时简单并模拟MyBatis中的像如下写法,解析下面代码的实现原理
@Select("SELECT username,email,newname,nick_name FROM user_model")
@Results({
@Result(property = "username", column = "username"),
@Result(property = "email", column = "email"),
@Result(property = "newname", column = "newname"),
@Result(property = "nickName", column = "nick_name")
})
List<UserModel> getAll();
这里定义一个自定义的注解接口 代码如下,注解要作用于方法上
Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CustomSelect {
String Sql() default "";
}
这一步也非常简单,定义一个操作接口,使用自定义的注解,添加好相关的Sql语句
public interface ReflectorDao {
@CustomSelect(Sql = "select * from user_model where id=1")
int InsertModel();
}
接下来就是怎么使用了,当然这里还是要用到反射 ,这里我添加了一个测试方法,里面模拟实现了一个JDBC的操作方法
@Test
public void testReflectorDao() throws Exception {
Class<?> c= ReflectorDao.class;
Method method=c.getMethod("InsertModel");
CustomSelect customSelect=method.getAnnotation(CustomSelect.class);
String strsql= customSelect.Sql();
System.out.print(strsql+"\r\n");
//调用JDBC完成操作
JDBCHelper.ExcuteQuery(strsql); }
这里反射里面的方法就不特殊说明了,这里说下 获取注解的方法把,如下
// // 获取某个类型的注解
// public <A extends Annotation> A getAnnotation(Class<A> annotationClass);
// // 获取所有注解(包括父类中被Inherited修饰的注解)
// public Annotation[] getAnnotations();
// // 获取声明的注解(但是不包括父类中被Inherited修饰的注解)
// public Annotation[] getDeclaredAnnotations();
// // 判断某个对象上是否被某个注解进行标注
// public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
//
// // 获取某个类声明的所有字段
// public Field[] getDeclaredFields() throws SecurityException;
// // 获取某个方法
// public Method getMethod(String name, Class<?>... parameterTypes);
执行下单元测试: OK


.NetCore Attribute
Java中有的 .NetCore一样能实现,这里就要介绍.NetCore总的特性了,就是Attribute类,怎么来使用这个呢?不要急,通过带来是解释,自定义的特性需要继承Attribute类,且类名可以以Attribute结尾,这样在使用的时候就可以通过前面的名称来写,代码如下
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class CustomAttribute : Attribute
{
public string Sql { get; set; } }
看到这里,就有一种熟悉感,看下源码,这里什么Class 等等更Java用法一样标识该特性作用于什么对象上,AllowMultiple :标识是否可以指定多次,同一个对象上多次使用特性,Inherited:当前特性是否可以被继承
//
// 摘要:
// Specifies the application elements on which it is valid to apply an attribute.
[Flags]
public enum AttributeTargets
{
//
// 摘要:
// Attribute can be applied to an assembly.
Assembly = ,
//
// 摘要:
// Attribute can be applied to a module.
Module = ,
//
// 摘要:
// Attribute can be applied to a class.
Class = ,
//
// 摘要:
// Attribute can be applied to a structure; that is, a value type.
Struct = ,
//
// 摘要:
// Attribute can be applied to an enumeration.
Enum = ,
//
// 摘要:
// Attribute can be applied to a constructor.
Constructor = ,
//
// 摘要:
// Attribute can be applied to a method.
Method = ,
//
// 摘要:
// Attribute can be applied to a property.
Property = ,
//
// 摘要:
// Attribute can be applied to a field.
Field = ,
//
// 摘要:
// Attribute can be applied to an event.
Event = ,
//
// 摘要:
// Attribute can be applied to an interface.
Interface = ,
//
// 摘要:
// Attribute can be applied to a parameter.
Parameter = ,
//
// 摘要:
// Attribute can be applied to a delegate.
Delegate = ,
//
// 摘要:
// Attribute can be applied to a return value.
ReturnValue = ,
//
// 摘要:
// Attribute can be applied to a generic parameter.
GenericParameter = ,
//
// 摘要:
// Attribute can be applied to any application element.
All =
}
下面我们同样用接口来实现,代码如下,前面说了 自定义属性已Attribute结尾的类名,写的时候直接写Custom
public interface RelactorDao
{ [Custom(Sql = "select * from user_model where id=1")]
void InsertModel(); }
这里我写一个测试类是看下,由于时间的关系,这里就不写SqlHelper 来执行了,输入下Sql就行了,这个跟Java一样需要使用反射,思想一样,只是使用方法名称不同而已,具体的方法就不做介绍..有兴趣自己了解下
public class TestClass
{
public void TestMethod()
{
var type = typeof(RelactorDao);
MethodInfo methodInfo= type.GetMethod("InsertModel");
var atrrs = methodInfo.GetCustomAttributes(typeof(CustomAttribute), false) as CustomAttribute[];
var strSql = atrrs.First().Sql;
//当然这里也可以执行
Console.WriteLine(strSql);
}
}
接下来我们在看下执行效果 OK

总结
这里只是简单的模拟下,其实要实现MyBatis中的注解的功能,其实还需要其他的知识,面向切面编程的技术AOP
Java中的 @Aspect 注解 ,.NetCore 可以通过动态代理来实现,但是反过来想下,.NetCore中一样可以实现类似于MyBait一样使用方式的ORM框架,可能.NetCore中考虑到大量应用反射会导致性能问题
Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute的更多相关文章
- Java Spring Boot VS .NetCore (一)来一个简单的 Hello World
系列文章 Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filte ...
- Java Spring Boot VS .NetCore (二)实现一个过滤器Filter
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (三)Ioc容器处理
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (五)MyBatis vs EFCore
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (七) 配置文件
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (十) Java Interceptor vs .NetCore Interceptor
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
随机推荐
- Java垃圾收集器概述
垃圾收集器的操作 查找未使用的对象,释放内存,并压缩堆,避免内存碎片 一个java程序,有执行应用程序逻辑的线程和执行GC的线程组.当GC跟踪对象引用,或在内存中移动对象,它必须确保应用程序线程没有使 ...
- Python——转义字符解释
转义字符 解释 ASCII值 \a 响铃 7 \b 退格 8 \f 换页 12 \n 换行 10 \r 回车 13 \t 水平制表 9 \v 垂直制表 11 \\ 一个反斜线字符 92 \' 一个单引 ...
- 7.docker日志收集
默认情况下,docker logs或者docker service logs显示命令的输出,就像在终端中以交互方式运行命令时一样.UNIX和Linux命令通常开在运行时间上三个I / O流,所谓的 S ...
- Web框架的原理和Django初识
一.Web框架的本质 1.本质 实际上Web应用本质上就是一个socket服务端, 而用户的浏览器就是一个socket客户端. 2.最原始的web框架 socket服务端 import socket ...
- 转:ls用法详解
在Linux中显示文件大小的时候,通常的做法是使用“ls -l”,显示的大小是文件的字节大小. 但是,如果文件比较大的话,显示起来不是特别易读,这个时候,可以使用“ls -lh”,就可以使用比较接近文 ...
- Django+Vue打造购物网站(四)
首页商品类别数据显示 商品分类接口 大概需要两个,一个显示三个类别 一个显示类别及类别下的全部商品 现在开始写商品的接口 首先编写三个分类的serializer class CategorySeria ...
- 【并发编程】一个最简单的Java程序有多少线程?
一个最简单的Java程序有多少线程? 通过下面程序可以计算出当前程序的线程总数. import java.lang.management.ManagementFactory; import java. ...
- scrapy之使用LinkExtractor提取链接
一.概述: 在页面含有少量链接时,使用selector来提取信息就可以,但如果链接特别多时,就需要用LinkExtractor来提取. 二.LinkExtractor构造器的各个参数: 1.allow ...
- 交互题[CF1103B Game with modulo、CF1019B The hat、CF896B Ithea Plays With Chtholly]
交互题就是程序与电脑代码的交互. 比如没有主函数的程序,而spj则给你一段主函,就变成了一个整体函数. 还有一种就是程序和spj之间有互动,这个用到fflush(stdout);这个函数就可以实现交互 ...
- Unity 动画系统
Legacy动画系统:Animation组件(旧) Mecanim动画系统:Animator组件(新) 动画播放过程: //动画片段 [System.Serializable] public clas ...