JAVA 6 ServiceLoader

I recently posted a question how to determine the implementation of an abstract service on the client side.

dfa is mentioned by java.util.ServiceLoader as a solution to my problem.

I ended up doing the same, although I didn’t use ServiceLoader directly, mainly because I used JDK 5. But another SOer jut panicked when dfa mentioned ServiceLoader.

I am wondering what are the main problems with implementing ServiceLoader. Despite its limitations, it seems that this is a good way to solve this problem, not paying attention to a third-party library like Guice

+5
source share
4 answers

ServiceLoader was added to java.util in JDK6, before that the basic technology was used in the Service class.

The ServiceLoader and DI interfaces solve similar problems, but are not equivalent technologies. ServiceLoader loads implementations of a specific interface found in the classpath. For example, if you have a program that reads Excel spreadsheets, and you find that the reader is able to read CSV files (which implement the same interface), you can drop the reader into the class path and make it accessible and selected as options in your program. (This means that your code is inherently more flexible).

( , Spring) , , , . Spring , , . , .

+4

ServiceLoader , , Spring Guice. , . ServiceLoader .

Tom Hawtin Tackline.

+2

sun.misc.Service

http://www.docjar.com/docs/api/sun/misc/Service.html

, J2SE API! Sun JDK. , , , JRockit.

+1

, ServiceLoader , , ( ). , , , DI , , @Inject .

, :

+1
source

All Articles