我需要通過外部jar提供一個接口實現的最簡單的方法。我想避免使用text/xml文件,因爲在重構類或接口名稱時可能會導致錯誤。Netbeans查找:使用註釋定義ServiceProvider而不是通過text/xml文件
我試過Netbeans查找,因爲它有一個@ServiceProvider annotation應該以聲明方式註冊實現。
所以我寫了一個簡單的試用碼,甚至沒有分裂接口和提供者在不同的瓶子,但它仍然無法查找組件:我錯誤地使用查詢
import org.openide.util.Lookup;
import org.openide.util.lookup.ServiceProvider;
public class LookupTrial {
public static void main(String[] args) {
IService s = Lookup.getDefault().lookup(IService.class);
if(s==null)
throw new RuntimeException("lookup failed");
else
s.hello();
}
public interface IService{
public void hello();
}
@ServiceProvider(service=IService.class)
public class MyImplementation implements IService{
public MyImplementation(){}
@Override
public void hello() {
System.out.println("hello lookup");
}
}
}
是誰?我應該搜索另一個查找庫來執行我想要的操作嗎?
我有同樣的問題 – dajood