Use of the SPI scenario __java

Source: Internet
Author: User

In the face of distributed development, many of the calls between the systems are using RPC direct call, but sometimes upstream systems need to call the downstream system many interfaces, resulting in a large amount of development work. Therefore, the upstream system uses SPI to play an SPI interface in the jar package, so that the downstream business implementation of this SPI interface, upstream system can be common to call this interface can use different functions.

implementation of downstream business side:

generally we need to design how to implement the SPI interface, the simplest way to achieve this is to implements this SPI interface, this implementation does not take advantage of subsequent extensions.

At present, in the project, I use the annotation +spring container launch characteristic + interface + interface template to implement.

like I need to implement upstream interface

Public Interface spiservice{

Public result<t> Process (Request request);

}

downstream of the implementation

Public class Spiserviceimpl implements spiservice{


private Spimanager Spimanager;

Public result<t> Process (Request request) {

return Spimanager.handle (Request);

}

}

Public Interface spimanager{

Public result<t> handle (request request);

}


Public class Spimanagerimpl extends Applicationobjectsupport implements spimanager{

private map<string,processspi> map=new hashmap<string,processspi> ();

protected void Initapplicationcontext (ApplicationContext context) throws Beansexception {
    Super.initapplicationcontext (context);

    Set up a different processor, and after the spring container starts, create a class mapping for the key in the annotation and the concrete implementation
     Map Set Value
}
Public result<t> handle (Request request) {
     Get the corresponding spiprocess class for processing according to the upstream key
}
    

}

public interface spiprocess{

Public Result doprocess (Request request);

}


public interface spiprocesstemplate{

Public before (request request,result<t> result);

Public process (Request request,result<t>result);

Public after (Request request,result<t>result);

}

Public abstract class abstractSpiprocess implements spiprocess,spiprocesstemplate{

Public result doprocess (Request request) {

Result result=new Result ();

before (request, result);

Process (request, result);

After (request, result); return result ;

}

}



The specific implementation of the business class, extends Abstractspiprocess can be used.



Finally, configure the class to the spring file, OK


This article simply said the use of SPI and simple implementation of the scenario. There is no more specific implementation, which requires different implementation frameworks based on different business designs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.