Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




Java Training: -Show Service Loader in Java?

What is Service loader in Java?

Service Loader is a new class added in Java 1.6 and it provides good means of developing loosely coupled application.

It allows us to link an interface to a particular implementation at run time based on configuration thus providing us with loose coupling

It also provides a sort of Dependency Injection (DI) though not as powerful as Spring framework but does provide a good facility in case you don’t require a full blown DI feature.

Also various design patterns like Strategy can be easily done with the help of Service Loader.

What is the Logic behind Service Loader?

Each interfaces present in the application is called service since they define what can be done but not how it can be done.

A single interface can have multiple implementations and each implementation is called service provider

Service Loader makes use of both the above concepts and to link a particular implementation with interface i.e. link a particular service with a service provider makes use of provider configuration file

Provider configuration file:

This file contains mapping of interface to implementation and is present in the resource directory META-INF/services

Within this file, contains a list of fully-qualified binary names of concrete provider classes, one per line and is compliant with UTF -8

Not clear yet, See example to make things crisp and clear

Let consider Account Interface as shown, We can see it has following methods

  • getType()
  • setType(String type)
  • getBalance()
  • getBranch()
  • setBalance(int balance)
  • setBranch(String branch)

Let say we have two implementing classes SavingAccount and CurrentAccount

We will create this example using Eclipse IDE. Following are the steps: -

1) Create a Java Project as shown. Give project name as ServiceLoaderEg

Figure: -

Figure: -

2) Create new package com.questpond.eg

Figure: -

3) Create interface Account within the above created package with discussed method signatures

Figure: -

4) Create class SavingAccount which implements Account. The implementation is shown

Figure: -

5) Similarly CurrentAccount

Figure: -

6) Create a folder META-INF within the project and within it create services folder. Within this create a file and name of the file should match fully qualified name of Account interface i.e. com.questpond.eg.Account

Figure: -

7) Within the file write the name of implementation you want to use . In the above example we have used the implementation as CurrentAccount. Again fully qualified name of CurrentAccount is needed to be put within the file. Same is shown in above figure

8) Create a main class called Main and within it write the following code

Figure: -

As we see in the screen shot we are making use of static load method of ServiceLoader and we have passed the class as interface type i.e. Account. The act of mapping this interface to corresponding implementation is done via the file which we created with META-INF/services folder

9) On running the above code we get following output

Figure: -

As we see from the screen shot the CurrentAccount implementation is used which is evident by the Sys out method printed and the type of Account

Note: Before running the example please copy META-INF folder completely within bin folder of eclipse

10) From the above example we see that the invoking class is not dependant directly on the Implementation and just depends on interface and the mapping between them is done by using ServiceLoader. Thus we achieve loosely coupled application

Application areas:

Many times when development happens cross team it is quite possible the team may have dependency on the code each other develop. So some classes if not completely developed can affect the dead lines of project as well testing. To do up with this we can create Mock objects corresponding to each classes which has not been fully developed and the mapping can be done by using ServiceLoader. Once the class is completely developed we can replace the Mock object with actual implementation via again Service Loader Thus we achieve Loosely coupled application as well Dependency Injection.

Download Source Code for Service Loader in Java.

Video on Service Loader:

Also see the following video which will clear your picture more on Service Loader.

More articles and videos coming:

I am going to write more articles as well as record more videos on Java interview questions and new rich features like concurrent collections. Semaphores, Executors and so on in coming time. So stay tuned

Regards,

Get more Java training stuffs from author's blog

Share this article   |    Print    |    Article read by 5166 times
Author:
Shivprasad Koirala
We conduct training for Java/J2EE technologies like design patterns, J2EE, Struts, Hibernate, Spring, Rich Interface etc. and likes to write articles on the same in free time.
Related Articles:
Related Interview Questions: