Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Play With Java ServiceLoader And Forget About Dependency Injection Frameworks

DZone's Guide to

Play With Java ServiceLoader And Forget About Dependency Injection Frameworks

Using Java SPI to load dependencies for Play Framework code using regular Java. No extra libraries or dependencies to add to your build.

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

In most of our applications, we are using Dependency Injection to loosely couple of our code. Sometimes, we just require simple DI, nothing else. For those, we need to include some of our DI frameworks like Spring, Google Guice, etc. This makes our project JAR heavy and added some unnecessary classes as well. For all these, Java itself has a ServiceLoader class to inject your dependency at runtime.

ServiceLoader was introduced in JDK 3, but this is used for internal purposes. During JDK 6, this class scopes to public but still, it is a final class — we are not able to extends its functionality.

ServiceLoader will play an important role in JDK 9, which we will discuss in our next post. Today, we are creating a simple Greetings application using ServiceLoader.

Step 1: Prerequisite

  • JDK >= 6

  • Maven

Step 2: Abstract Layer

Create an abstract layer for any service.

package com.knoldus.service;

public interface GreetingsService {
    void sayHello(String name);
}


Create an implementation of the service by implementing the interface as below:

package com.knoldus.service.impl;

import com.knoldus.service.GreetingsService;

public class ConsoleGreetings implements GreetingsService {
    @Override
    public void sayHello(String name) {
        System.out.println("Hello to "+name);
    }
}


Step 3: Define the Provider

Now we need to define our provider, which loads the GreetingsService implementation at runtime and creates an instance to it.

import java.util.ServiceLoader;

public class GreetingsProvider {
    private static GreetingsProvider provider;
    private ServiceLoader<GreetingsService> loader;

    private GreetingsProvider() {
        loader = ServiceLoader.load(GreetingsService.class);
    }

  public static GreetingsProvider getInstance() {
      if(provider == null) {
          provider = new GreetingsProvider();
      }

      return provider;
    }

    public GreetingsService serviceImpl() {
        GreetingsService service = loader.iterator().next();

        if(service != null) {
            return service;
        } else {
            throw new NoSuchElementException(
            "No implementation for GreetingsProvider");
        }
    }
}


Note: To load GreetingService, we are using ServiceLoader. If GreetingsService has multiple implementations, the ServiceLoader will load all of them. This depends on us and how we can handle implementations. In the above code, I am assuming there is only one implementation and finding out using next().

Step 4: Create a Directory

Creating a directory (META-INF/service) on your classpath. That directory contains a file with the fully qualified name of your abstract service class, like com.knoldus.service.GreetingsService.

This files contains the details of its implementation: com.knoldus.service.impl.ConsoleGreetings.

Step 5: Create a Main Class

Create a main class for the code execution as below:

package com.knoldus.main;

import com.knoldus.providers.GreetingsProvider;
import com.knoldus.service.GreetingsService;

public class Launcher {
    public static void main(String... args) {
        GreetingsService service = GreetingsProvider.getInstance().serviceImpl();
        service.sayHello("Knoldus");
    }
}

References:

https://docs.oracle.com/javase/tutorial/ext/basics/spi.html

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,dependency injection ,serviceloader ,frameworks

Published at DZone with permission of Harmeet Singh(Taara). See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}