implementation 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 特性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值