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