ServiceLoader to find implementations of an interface

后端 未结 3 2216
孤街浪徒
孤街浪徒 2020-12-24 03:39

I tried to use the Java ServiceLoader to find all classes that implement a specific interface like so:

loader = ServiceLoader.load(Operation.class);
try {
           


        
3条回答
  •  攒了一身酷
    2020-12-24 03:59

    If the implementations are ones that you wrote yourself, you could use AutoService to make them available through the ServiceLoader interface, eg

    @AutoService(Operation.class)
    class Foo implements FooInterface {
    
    }
    
    @AutoService(Operation.class)
    class Bar extends Foo {
    
    }
    

提交回复
热议问题