假设在我们的机器上调试程序时,我们无法访问某些所需的服务器。所以我们需要一个存根。但是每次我想要make build而没有存根我需要注释我的存根代码并取消注释实际代码,所以它看起来很脏。如果我能以某种方式配置构建以避免此注释/取消注释,那将是很好的。我还没有想出任何好的决定。
例如,一些代码
public class SingeFormatServiceClient {
public static final QName SERVICE_NAME = new QName("http://creditregistry.ru/2010/webservice/SingleFormatService", "SingleFormatService");
public SingleFormatService Connect(){
URL wsdlURL = SingleFormatService_Service.WSDL_LOCATION;
SingleFormatService_Service ss = new SingleFormatService_Service(wsdlURL, SERVICE_NAME);
return ss.getSingleFormatServiceHttpPort();
}
public SingleFormatService Connect(){
return new SingleFormatServiceStub();
}
}
所以第一个功能是实际的,第二个是存根。可能有一种方法不评论,但只是对建设者说,现在我想用第一个功能构建,现在 - 第二个? 谢谢。
答案 0 :(得分:1)
使用System.getProperty()
来实例化实现。例如:
SingleFormatService service = (SingleFormatService) Class.forName(
System.getProperty("single_format_service_class",
"your.comp.SingleFormatServiceStub")).getConstructor().newInstance();
您的实现必须提供非arg构造函数。在你的jvm参数中,指定工作类,即
-Dsingle_format_service_class=your.comp.SingleFormatServiceActual
在intellij idea中,您可以使用不同的jvm args指定多个运行配置。
NB。 IMO许多图书馆都是这样使用的。 Hibernate使用hibernate.cache.provider_class
来选择要使用的缓存提供程序实现。