Serviceloader and ClassLoader are 2 different and interconnected loaders in Java. The JVM uses ClassLoader to load classes into memory, which is the first step in a class declaration cycle (The complete lifecycle of a Java class goes through loading, connecting, Initialize, use, and uninstall five stages, of course, there are cases that are used directly after loading or connecting without being initialized. For more information, see: The Lifecycle of Java classes
What about the Serviceloader? Serviceloader: A simple service provider loading facility. A service is a collection of familiar interfaces and classes (usually abstract classes). A service provider is a specific implementation of a service. The class in the provider typically implements the interface and subclasses the subclasses defined in the service itself. Service providers can be installed in an extended form in the implementation of the Java platform, which is to put jar files into any common extended directory. You can also make a provider available by adding it to the application classpath, or through some other platform-specific way. ...... The only compelling requirement is that the provider class must have a constructor method without parameters so that they can be instantiated in the load.
Identify the service provider by placing the provider configuration file in the resource directory meta-inf/services. The file name is the fully qualified binary name of the service type. The file contains a list of fully qualified binary names for a specific provider class, one per line. Ignores spaces, tabs, and blank lines around each name. The annotation character is ' # ' (' \u0023 ', number SIGN), ignoring all characters after the first comment character in each line. The file must be encoded using UTF-8.
Find and instantiate providers in a deferred manner, that is, as needed. The service loader maintains a provider cache that has been loaded so far. Each call to the iterator method returns an iterator that first generates all the cached elements in an instantiated order, and then finds and instantiates all remaining providers in a deferred manner, adding each provider to the cache in turn. The cache can be cleared by the reload method.
First of all, this class is final and cannot be inherited. Often we will have an interface many implementation classes, such as: Userdao interface has USERDAOIMPL1, UserDaoImpl2 .... And many other implementations. At this time we can only own new, but with the serviceloader<s> is very convenient, he will help us put the implementation class into a set for easy traversal. But what's the use of this, the command mode is a big use:
First create a Meta-inf folder underneath the SRC folder and create a Services folder with a full class name file for your interface, like Org.web.UserDao.
And the file contains the implementation class for this interface, such as ORG.WEB.USERDAOIMPL1 ORG.WEB.USERDAOIMPL2
And then a code is OK.
And then you can go through the list.
Next, let's introduce the common uses of this serviceloader.
Like we're going to judge the type of value that comes in.
if (a = = 1) {
//processing thing
}
else if (a = = 2) {/
/handling thing
}else if (a = = 2) {/
/handling thing
In this case, there will be more than one. else if is not conducive to expansion
So you can make each if a command mode
First create an excuse Interface
public interface inte{
//Determine if the solution to this implementation class is public,
boolean is (int num);
Solution public
void result ();
Then create 2 implementation classes
public class INTEIMPL1 implements inte{public
boolean is (int num) {
//replaces multiple if
if (num = 1)
return true;
return false;
public
void result () {
///Here is the 1 solution
}
public class INTEIMPL2 implements inte{public
boolean is (int num) {
//replaces multiple if
if (num = 2)
return true;
return false;
public
void result () {
///Here is the 2 solution
}
Write a full class name for the corresponding interface and implementation class in services:
Xx.xx.inteImpl1
Then you can use the following:
Serviceloader<inte> list=serviceloader.load (Inte.class); For
(inte in:list) {
if (in.is (1)) {
in.result ();
}
So later to expand to write an implementation class, add a configuration, better embodies the object-oriented thinking.
Reference: http://blog.csdn.net/lmh12506/article/details/50428860
Http://www.cnblogs.com/nimendezaige/p/4866468.html