微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Ximplementation 示例扩展库 基于Java注解的调用时路由框架

程序名称:Ximplementation 示例扩展库

授权协议: Apache-2.0

操作系统: 跨平台

开发语言: Java

Ximplementation 示例扩展库 介绍

Ximplementation是一个基于Java注解的调用时路由框架。

包括两个核心注解:

@Implementor
此注解标注于类,表明类是某个或者某些类的实现类,就像implements和extends关键字。

@Implement
此注解标注于@Implementor类的方法,表明方法实现方法,就像@Overriden注解。

示例

假设有一个接口类如下:

public interface Service
{
    T plus(T a, T b);
    T minus(T a, T b);
}

那么,你可以自由地编写它的实现类:

public class ServiceImplDefault implements Service
{
    public T plus(T a, T b){...}
    public T minus(T a, T b){...}
}

public class ServiceImplPlusInteger implements Service
{
    @Override
    public Integer plus(Integer a, Integer b){...}

    @NotImplement
    @Override
    public Integer minus(Integer a, Integer b){ throw new UnsupportedOperationException(); }
}

@Implementor(Service.class)
public class ServiceImplMinusInteger
{
    @Implement
    public Integer minus(Integer a, Integer b){...}
}

注:ServiceImplMinusInteger并不是必须的。而且,你可以在同一个或者多个其他@Implementor实现类内为plus和/或minus编写多个实现方法

之后,你可以通过如下方式获得Service的实例:

Implementation implementation = new ImplementationResolver().resolve(Service.class,
                ServiceImplDefault.class, ServiceImplPlusInteger.class, ServiceImplMinusInteger.class);

Implementorbeanfactory implementorbeanfactory = SimpleImplementorbeanfactory
            .valueOf(new ServiceImplDefault(), new ServiceImplPlusInteger(), new ServiceImplMinusInteger());

Service service = new ProxyImplementeeBeanBuilder().build(implementation, implementorbeanfactory);

对于serivce.plus方法调用,如果参数类型是Integer,将被路由至ServiceImplPlusInteger.plus方法,否则,将被路由至ServiceImplDefault.plus方法;对于serivce.minus方法调用,如果参数类型是Integer,将被路由至ServiceImplMinusInteger.minus方法,否则,将被路由至ServiceImplDefault.minus方法

扩展库

Ximplementation-spring是一个ximplementation 整合至Spring的库,使Spring能够支持多实例依赖注入和更多
ximplementation 特性。

点击此处查看详细ximplementation-
spring介绍

点击此处下载ximplementation-
spring

Ximplementation 示例扩展库 官网

https://github.com/ximplementation/ximplementation

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐