5 things you didn't know about ... everyday Java tools
2. ServiceLoader
Java developers frequently wish to decouple the knowledge necessary to use a component from the knowledge necessary to create one. This is typically accomplished by creating an interface describing the actions the component can perform, and using some kind of intermediary to create the component instances. Many developers use the Spring framework for this purpose, but there's another approach that is even more lightweight than a Spring container.
The ServiceLoader
class from java.util
can read a configuration file tucked away in a JAR file and find implementations of an interface, then make those implementations available as a list of objects to choose from. If you needed a personal-servant component to carry out your tasks, for instance, you could acquire one with the code in Listing 2:
Read full article from 5 things you didn't know about ... everyday Java tools
No comments:
Post a Comment