You should understand the mechanism of Java SPI

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 Javathe SPImechanism. 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.

cicada8-spi.md---0082zybply1gc6sqv3gp4j30zm0u0n8c.jpg

Would let us look at the difference between the two to achieve, starting with the extent of the code is simple, it is SPIeven better.

What is SPI

Under prior specific analysis or to understand SPIwhat is?

First, it is actually Service provider interfaceshorthand, 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 IOCthe 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:

cicada8-spi.md---0082zybply1gc6tlhql39j31490u0wjj.jpg

Which contains a number of Beancontainers necessary operations: registration, access, release bean.

In order to let other people can realize their IOCcontainer, so we will this interface into a single Module, the others for the introduction of implementation.

cicada8-spi.md---0082zybply1gc6tobsdgwj30u40ewdh1.jpg

So when I want to achieve a single example of IOCthe container, I just need to create a new Moduleand just introduced modules and implement CicadaBeanFactorythe interface can be.

Of course, the most important is the need resourcesto create a new directory META-INF/services/top.crossoverjie.cicada.base.bean.CicadaBeanFactoryfile, the file name have to be before we define the fully qualified name of the interface (SPI specification).

cicada8-spi.md---0082zybply1gc6ts164zlj30uk0amq3x.jpg

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 classpaththere is a realization that we have just class (introduced jar package implementation class), it can java.util.ServiceLoaderbe 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 IOCcontainer 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, Dubboin providing a range of extensions:

cicada8-spi.md---0082zybply1gc6ue6zubvj30gq0pymyq.jpg

The same type of RPCframe motanis also provided in the extended response:

cicada8-spi.md---0082zybply1gc6ufacqt5j30lm0j8q5j.jpg

Their use Java SPI and both are very similar, but slightly different principle, but also adds new functionality.

For example motanthe spipermit is a single embodiment and the like.

Another example MySQL driver package is to realize their connection logic using SPI.

cicada8-spi.md---0082zybply1gc6uqg2ga2j30ii0bmdgz.jpg

to sum up

JavaIts SPIactually a bit small problems, such as:

  • Traversal implementation class to load all low efficiency.
  • When multiple ServiceLoaderat the same time loadthere will be concurrent problem (although no one to do it).

To sum up, SPIis 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 SPIis also very useful.

All paper Source:

github.com/TogetherOS/…

Share your thumbs and is the biggest support for me

Guess you like

Origin juejin.im/post/5e5316f0e51d452728646ce8