Dubbo源码解读:appendAnnotation [01]
import java.lang.reflect.Method; public class AppendAnnotation {
/**
* 获取某个Annotation实例的所有方法值(实际是Annotation的属性值),并把这些方法的值赋值给调用此方法的类的相关的setter方法。
*/
protected void appendAnnotation(Class<?> annotationClass, Object annotation) {
Method[] methods = annotationClass.getMethods(); // 从当前类或接口中(包括父类或父接口)获取公开(public)的方法
for (Method method : methods) {
if (method.getDeclaringClass() != Object.class // 方法所在类不是Object本身
&& method.getReturnType() != void.class // 方法的返回类型不是void
&& method.getParameterTypes().length == 0 // 方法的参数个数为0
&& Modifier.isPublic(method.getModifiers()) // 方法的修饰符为public
&& !Modifier.isStatic(method.getModifiers())) // 方法不是静态的
{
try {
String property = method.getName(); // 方法名称 // 如果方法的名称为“interfaceClass”或者interfaceName”,则统一把方法名称:property的值设置为“interface”
if ("interfaceClass".equals(property) || "interfaceName".equals(property)) {
property = "interface";
} // 构造set方法的完整名称,比如:set+V+ersion=setVersion、set+G+roup=setGroup等等
String setter = "set" + property.substring(0, 1).toUpperCase() + property.substring(1); // 通过反射的方式来调用底层方法,annotaion为被调用方法所在的对象(这里是Annotaion对象),value为方法返回值,实际上就是我们设置的annotaion的属性值。
Object value = method.invoke(annotation); // 如果属性值不为null并且不是方法返回值的默认值
if (value != null && !value.equals(method.getDefaultValue())) { // 把方法返回值的类型进行装箱
Class<?> parameterType = ReflectUtils.getBoxedClass(method.getReturnType()); // 如果方法名称(属性名称)为filter或listner,annotation中filter和listner定义的是string[]类型
if ("filter".equals(property) || "listener".equals(property)) {
parameterType = String.class;
value = StringUtils.join((String[]) value, ",");
} else if ("parameters".equals(property)) { // String[] parameters() default
// {};这种情况下把string[]转换为Map
parameterType = Map.class;
value = CollectionUtils.toStringMap((String[]) value);
}
try {
// 获取AppendAnnotation方法所在类的setter名称所对应的方法(比如:ReferenceConfig类型的setConnections方法,参数值类型为:Integer)
Method setterMethod = getClass().getMethod(setter, parameterType); // 通过反射的方式来调用setter方法(相当于new ReferenceConfig().setConnections(1)这样)
setterMethod.invoke(this, value);
} catch (NoSuchMethodException e) {
// ignore
}
}
} catch (Throwable e) {
logger.error(e.getMessage(), e);
}
}
}
}
}
Dubbo源码解读:appendAnnotation [01]的更多相关文章
- Dubbo源码解读
1.提升SOA的微服务架构设计能力 通过读dubbo源码是一条非常不错的通往SOA架构设计之路,毕竟SOA的服务治理就是dubbo首先提出来的,比起你去看市面上的SOA微服务架构的书籍,学到的架构 ...
- prototype.js 源码解读(01)
prototype.js是一个设计的非常优雅且很有实用价值的js基础类库,其源码非常值得研究.研究它的源码不仅能提升个人水平,而且对你打下坚实的js基础也很有帮助.因本人技术水平有限,该解读仅供参考. ...
- 【Dubbo 源码解析】07_Dubbo 重试机制
Dubbo 重试机制 通过前面 Dubbo 服务发现&引用 的分析,我们知道,Dubbo 的重试机制是通过 com.alibaba.dubbo.rpc.cluster.support.Fail ...
- 【Dubbo 源码解析】06_Dubbo 服务调用
Dubbo 服务调用 根据上图,可以看出,服务调用过程为: Consumer 端的 Proxy 调用 Cluster 层选择集群中的某一个 Invoker(负载均衡) Invoker 最终会调用 Pr ...
- 【Dubbo 源码解析】05_Dubbo 服务发现&引用
Dubbo 服务发现&引用 Dubbo 引用的服务消费者最终会构造成一个 Spring 的 Bean,具体是通过 ReferenceBean 来实现的.它是一个 FactoryBean,所有的 ...
- 【Dubbo 源码解析】04_Dubbo 服务注册&暴露
Dubbo 服务注册&暴露 Dubbo 服务暴露过程是通过 com.alibaba.dubbo.config.spring.ServiceBean 来实现的.Spring 容器 refresh ...
- 【Dubbo 源码解析】03_Dubbo Protocol&Filter
Protocol & Filter Dubbo 服务暴露和服务引用都是通过的 com.alibaba.dubbo.rpc.Protocol 来实现的.它是一个 SPI 扩展. @SPI(&qu ...
- 【Dubbo 源码解析】02_Dubbo SPI
Dubbo SPI:(version:2.6.*) Dubbo 微内核 + 插件 模式,得益于 Dubbo SPI .其中 ExtentionLoader是 Dubbo SPI 最核心的类,它负责扩展 ...
- AFNetworking 3.0 源码解读 总结(干货)(下)
承接上一篇AFNetworking 3.0 源码解读 总结(干货)(上) 21.网络服务类型NSURLRequestNetworkServiceType 示例代码: typedef NS_ENUM(N ...
随机推荐
- 监听Listview的滚动状态,是否滚动到了顶部或底部
/** * @author:Jack Tony * @description : 监听listview的滑动状态,如果到了顶部就刷新数据 * @date :2015年2月9日 */ private c ...
- 用SwipeBackLayout实现滑动关闭当前Activity
说起SwipeBackLayout,我对它还是有一定怨念的.当时就希望能实现关闭当前Activity的效果,但完全搜不当相关的东西,最后好不容易搜到了这个SwipeBackLayout,觉得可以实现滑 ...
- Orchard之模版开发
生成新模版之后(参看:Orchard之生成新模板),紧接着就是模版开发了. 一:开发必备之 Shape Tracing 到了这一步,非常依赖一个工具,当然,它也是 Orchard 项目本身的一个 Mo ...
- 升级pip后出现 ImportError: cannot import name main
原文链接 https://blog.csdn.net/accumulate_zhang/article/details/80269313 在Ubuntu中,升级了pip,再次使用pip 安装相关的 ...
- Solr搜索结果说明 (转)
在admin页面,输入相关内容后,会返回xml格式的内容.说明如下: <?xml version="1.0" encoding="UTF-8"?> ...
- [leetcode]Restore IP Addresses @ Python
原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...
- [leetcode]Pascal's Triangle II @ Python
原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...
- MongoDB 通过配置文件启动
mongod启动命令需指定一些参数启动服务,我们通过配置文件的方式配置这些参数加载配置. ./bin/mongod --config ./conf/mongodb.conf(或./bin/mongod ...
- ORACLE中union/union all/Intersect/Minus用法
Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序: Union All,对两个结果集进行并集操作,包括重复行,不进行排序: Intersect,对两个结果集进行交集操作,不包 ...
- 云服务器 ECS Linux Ubuntu 主机修改主机名
云服务器 ECS Linux 主机修改主机名 修改云服务器 ECS Linux 主机名常见的有两种方式,本文对此进行概要说明. 临时生效修改 使用命令行修改 hostname 主机名(可自定义),重新 ...