Foreword
I do not know now there is no company to return to work, I've been working from home for nearly three weeks, but also at home for a month; okay job is not affected in any way, I personally have always felt that telework and IT industry is very fit, the efficiency of this time even higher than in the office, and because our business overseas, so the epidemic caused little too much influence.
Pulling away, this time mainly want to share with you Java
the SPI
mechanism. Weekend lacks something, I flipped through the blog I wrote before "IOC container design a pluggable" , discovered that the implementation is not so elegant.
I have not seen a friend to be a prospect summary, when the demand:
I implemented a framework similar to http SpringMVC but very lightweight Cicada , which of course requires an IOC container that can store all singleton bean.
This implementation IOC container I hope to have a variety of ways and can even provide an interface for others to achieve; of course, the process of switching the IOC container is certainly not exist hard-coded, which is mentioned here pluggable . When I want to use implementations A, I introduced the jar package A, B on the introduction of the package when using B.
Would let us look at the difference between the two to achieve, starting with the extent of the code is simple, it is SPI
even better.
What is SPI
Under prior specific analysis or to understand SPI
what is?
First, it is actually Service provider interface
shorthand, translated into Chinese is a service provider discovery interface.
But here Do not be confused this term, here 服务发现
, and we often hear of micro-service and service discovery can not be equated.
Like I mentioned above for IOC
the container more implementations A, B, C (they can be understood as a service), I need to know which particular implementation used at runtime.
In fact, in essence this is a typical interface-oriented programming, which is when we started to learn programming was repeatedly emphasized.
SPI practice
Next we come to realize how to use SPI just mentioned pluggable IOC container.
Now just mentioned is the essence of SPI interface-oriented programming, so naturally we first need to define an interface:
Which contains a number of Bean
containers necessary operations: registration, access, release bean.
In order to let other people can realize their IOC
container, so we will this interface into a single Module
, the others for the introduction of implementation.
So when I want to achieve a single example of IOC
the container, I just need to create a new Module
and just introduced modules and implement CicadaBeanFactory
the interface can be.
Of course, the most important is the need resources
to create a new directory META-INF/services/top.crossoverjie.cicada.base.bean.CicadaBeanFactory
file, the file name have to be before we define the fully qualified name of the interface (SPI specification).
The contents of which is our own class that implements the fully qualified name:
top.crossoverjie.cicada.bean.ioc.CicadaIoc
复制代码
Imagine creating objects will eventually be reflected by the fully qualified name here.
But this process has provided Java API shut off:
public static CicadaBeanFactory getCicadaBeanFactory() {
ServiceLoader<CicadaBeanFactory> cicadaBeanFactories = ServiceLoader.load(CicadaBeanFactory.class);
if (cicadaBeanFactories.iterator().hasNext()){
return cicadaBeanFactories.iterator().next() ;
}
return new CicadaDefaultBean();
}
复制代码
When classpath
there is a realization that we have just class (introduced jar package implementation class), it can java.util.ServiceLoader
be found all tools implementation class (the class may have multiple implementations exist, but usually we only need one).
After a number of ready to use naturally very simple.
<dependency>
<groupId>top.crossoverjie.opensource</groupId>
<artifactId>cicada-ioc</artifactId>
<version>2.0.4</version>
</dependency>
复制代码
We just need to be able to rely on the introduction of the use of its realization, when we want to change an implementation only need to replace a dependency can be.
This did not modify the line of code Flexible 可拔插
selected IOC
container of.
SPI's other applications
Although usually not be used directly to SPI to achieve business, but in fact the vast majority of framework we used SPI will provide convenient user interface to expand their capabilities.
For example, Dubbo
in providing a range of extensions:
The same type of RPC
frame motan
is also provided in the extended response:
Their use Java SPI and both are very similar, but slightly different principle, but also adds new functionality.
For example motan
the spi
permit is a single embodiment and the like.
Another example MySQL driver package is to realize their connection logic using SPI.
to sum up
Java
Its SPI
actually a bit small problems, such as:
- Traversal implementation class to load all low efficiency.
- When multiple
ServiceLoader
at the same timeload
there will be concurrent problem (although no one to do it).
To sum up, SPI
is not an advanced technology, is essentially oriented programming interface, while the interface itself facing in our daily development is also an essential skill, understanding use SPI
is also very useful.
All paper Source:
Share your thumbs and is the biggest support for me