The Java ServiceLoader is a utility that enables applications to discover and load service implementations at runtime. By defining a service interface and providing one or more implementations of it, ServiceLoader simplifies the process of loosely coupling components in an application. Below is a simple example demonstrating how to use ServiceLoader to load services in Java.
import java.util.ServiceLoader;
// Define a service interface
public interface GreetingService {
void greet(String name);
}
// Implement the service interface
public class EnglishGreetingService implements GreetingService {
public void greet(String name) {
System.out.println("Hello, " + name + "!");
}
}
// Main application to load the service
public class ServiceLoaderExample {
public static void main(String[] args) {
ServiceLoader serviceLoader = ServiceLoader.load(GreetingService.class);
for (GreetingService service : serviceLoader) {
service.greet("John Doe");
}
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?