gold問題集の問題62の回答に、選択肢B)module clientでrequires provider;する必要がある
というのが回答になっている、が、切らなくても正常に動く。
requiresはあくまでもmoduleとしての依存であって、class依存ではない認識
usesは公開というよりも使用する、消費するという意味。
ちなみに1モジュールのmodule-info.javaではprovideを1個しかかけないようだが
providerモジュールを追加すれば、そちらでもprovideできる。
その場合、ServiceLoader.load(SampleInterface.class)は、provideされた2個の
implが登録されている。for(xx:ServiceLoader x)で回してやると2個分のimpleが
動かせる。
自分はこんな環境で実行できた。
Api\module-info.java
module Api {
exports api;
}
Provider\module-info.java
module Provider {
requires Api;
provides api.SampleInterface with provider.SampleImplClass;
}
Client\module-info.java
module Client {
requires Api;
uses api.SampleInterface;
}
api.Api.java
public interface SampleInterface{
void method();
}
provider.SampleImplClass.java
public class SampleImplClass implements SampleInterface{
@Override
public void method() {
System.out.println("sampleimplclass method");
}
}
client.Client.java
public class Client {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ServiceLoader<SampleInterface> sl = ServiceLoader.load(SampleInterface.class);
for(SampleInterface si:sl){
si.method();
}
}
}