当我移动 Web 服务时,所有硬编码 URI 会发生什么情况?

发布于 2024-07-19 14:49:09 字数 691 浏览 5 评论 0原文

我正在使用 JAX-WS 开发一个测试 WS,演示工作正常,但如果我尝试将其转移到生产环境中,我的代码中会出现很多测试环境的 URL。 例如:

com.mycompany.testserver.ws.writer.WriterInterface service = new com.mycompany.testserver.ws.writer.WriterInterface();

QName portQName = new QName("http://testserver.mycompany.com/ws/writer.php", "WriterInterfacePort");

String req = "SOME_XML_HERE";

try { // Call Web Service Operation

    Dispatch<Source> sourceDispatch = null;
    sourceDispatch = service.createDispatch(portQName, Source.class, Service.Mode.PAYLOAD);
    Source result = sourceDispatch.invoke(new StreamSource(new StringReader(req)));
} catch (Exception ex) [
//do stuff here
}

移动此类应用程序的最佳实践是什么?

I'm developing a test WS with JAX-WS and the demo works fine but if I try to move it in production there are a lot of occurences of the URL of my test enviroment on my code. For example:

com.mycompany.testserver.ws.writer.WriterInterface service = new com.mycompany.testserver.ws.writer.WriterInterface();

QName portQName = new QName("http://testserver.mycompany.com/ws/writer.php", "WriterInterfacePort");

String req = "SOME_XML_HERE";

try { // Call Web Service Operation

    Dispatch<Source> sourceDispatch = null;
    sourceDispatch = service.createDispatch(portQName, Source.class, Service.Mode.PAYLOAD);
    Source result = sourceDispatch.invoke(new StreamSource(new StringReader(req)));
} catch (Exception ex) [
//do stuff here
}

What is the best practice for moving such an app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨敛清荷 2024-07-26 14:49:10

从配置文件中读取连接 URL。 您可能还希望有一个强类型的配置对象来缓存该值,以便只需要读取一次。 然后,当您将应用程序从测试转移到生产时,您只需要更新配置文件。

Read the connection URL from a configuration file. You might also want to have a strongly-typed configuration object that will cache the value so that it only needs to be read once. Then when you move your app from test to production, you only need to update the configuration file.

关于从前 2024-07-26 14:49:10

最常见的做法是将 URL 存储在配置文件中而不是硬编码,并为测试和生产提供单独的配置。

引入集成代理或服务注册中心往往有些矫枉过正。

The most common practice is to store the URL's in config files rather than hardcoded, and have separate configurations for test and production.

Introducing an integration broker or service registry tends to be overkill.

紫﹏色ふ单纯 2024-07-26 14:49:10

您确实应该避免将 URL 硬编码到代码中。 有多种方法可以避免这种情况。 即使您没有可用的配置文件功能,您至少应该将此数据存储在声明其的每个类顶部的字符串中。

无需告诉您(但我想我是)您现在遇到了为什么从不硬编码 URL/配置数据的原因。

You should really avoid hard coding URL's into code. There are a variety of ways this could be avoided. Even if you had no config file functionality available you should at the very least be storing this data in a string at the top of each class where its declared.

No need to tell you (But I guess I am) you are now encountering the reason as to why you never hard code URL's / configuration data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文