1

Let's say I have a service interface com.example.Service.

For a test, I defined a

src/test/resources/META-INF/services/com.example.Service

file which contains

com.example.sometest.MockServiceImpl

the MockServiceImpl implements the interface.

Now, when doing a ServiceLoader.load(Service.class) and iterating over the result, my MockServiceImpl is always instantiated again. The javadoc for ServiceLoader states that it DOES maintain a cache, but my mock service does not seem to be cached.

Why is that so, and how do I get the ServiceLoader to cache it?

rabejens
  • 7,594
  • 11
  • 56
  • 104

1 Answers1

2

I think API means that cache is per ServiceLoader instance: "Each invocation of the iterator method returns an iterator that first yields all of the elements of the cache...".

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275