"Java" uses Java SPI Serviceloader for modular development of Java application plug-ins

Source: Internet
Author: User

background: in the business customization needs to consider different interfaces of the service implementation, each ringtones requirements of the interface is generally inconsistent, require interface services can be plug-in way to provide;

Scenario Analysis:

1) using the OSGi framework for development, but considering that the OSGi framework is too heavy to introduce more things, abandon the program;

2) using the Java class loader dynamic loading external jar mechanism, dynamically loading the custom interface service class, this way is more complex, need to complete the specified interface service class file load, and need to be able to find all the interface service class, as an alternative;

3) Using the Java Service Provider Interface (SPI) mechanism to build a plug-in Java application framework, without introducing a new external framework, the implementation is simple, the most preferred solution

The following describes how to implement the plug-in service framework development process using Java SPI:

1) Create an interface service engineering Spi-service, which provides a service interface class or abstract class, all custom interface service classes implement or inherit the interface to complete the customization of the business;

2) Create an interface to implement the service engineering Spi-cn-service, which provides a custom implementation class for the service interface; When exported as a jar file, the Meta-inf directory and the files it contains are included in the jar file;

3) Create an Interface service test engineering spi-service-client, which provides a customized service based on the SPI Serviceloader mechanism;

The project requires that the project created by the 1 and 2) steps be packaged as a jar file to be added to the project's Classpath

4) Engineering

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/46/68/wKioL1PyGSyCk5EUAANit339b3w177.jpg "title=" Spi.png "alt=" Wkiol1pygsyck5euaanit339b3w177.jpg "/>


5) Itimeservice.java

Package Hxb.spi.service;public Abstract class Itimeservice {private string Servicename;public abstract string GetCurrentTime ();p ublic String getservicename () {return serviceName;} public void Setservicename (String serviceName) {this.servicename = ServiceName;}}

6) Cntimeservice.java

Package Hxb.spi.service;import Java.text.simpledateformat;import Java.util.date;public class CNTimeService extends Itimeservice {public Cntimeservice () {super.setservicename ("Cn-time-service");} @Overridepublic String GetCurrentTime () {SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); return Getservicename () + ":" +sdf.format (New Date ());}}

7) Ustimeservice.java

Package Hxb.spi.service;import Java.text.simpledateformat;import Java.util.date;public class USTimeService extends Itimeservice {public Ustimeservice () {super.setservicename ("Us-time-service");} @Overridepublic String GetCurrentTime () {SimpleDateFormat SDF = new SimpleDateFormat ("mm/dd/yyyy HH:mm:ss"); return Getservicename () + ":" +sdf.format (New Date ());}}

8) Meta-inf/services/hxb.spi.service.itimeservice File

Hxb.spi.service.CNTimeServicehxb.spi.service.USTimeServ

9) test class Spiclient.java

Package Hxb.spi.test;import Hxb.spi.service.itimeservice;import Java.util.serviceloader;public class SPIClient { public static void Main (string[] args) {serviceloader<itimeservice> Sloader = Serviceloader.load ( Itimeservice.class); for (Itimeservice Itimeservice:sloader) {System.out.println (Itimeservice.getcurrenttime ());}}}


This article is from the "Cape Technology" blog, please be sure to keep this source http://kypulo.blog.51cto.com/9227739/1541802

Related Article

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.