基于Java注解的调用时路由框架 Ximplementation

码农软件 · 软件分类 · Java开发工具 · 2019-11-06 11:28:23

软件介绍

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

本文地址:https://codercto.com/soft/d/18386.html

《Hello Ruby:儿童编程大冒险》(平装)

《Hello Ruby:儿童编程大冒险》(平装)

(芬兰)琳达·刘卡斯 / 窝牛妈 / 浙江人民美术出版社 / 2018

快来认识Ruby——一个想象力丰富,喜欢解决难题的女生。Ruby认识了一群新朋友:聪明的雪豹、友好的狐狸、忙碌的机器人等等。这本书以讲故事的方式向孩子们介绍了基础的计算思维,比如拆分问题,制定分步计划,寻找规律,打破思维定势等等;之后,通过一系列鼓励探索和创造的练习和活动,孩子们对这些关乎编程核心问题的基本概念有了进一步的理解。一起来看看 《《Hello Ruby:儿童编程大冒险》(平装)》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具