gpt4 book ai didi

java - 在 Java 8 中,如何在我的环境中不对其进行硬编码来获取我的主机名?

转载 作者:IT老高 更新时间:2023-10-28 13:49:34 25 4
gpt4 key购买 nike

我们刚刚在 Amazon Linux 上升级到 Java 8。我们正在使用 Spring 4.3.8.RELEASE。过去我们可以通过在我们的应用程序上下文文件中设置 bean 来获取我们的机器主机名,就像这样......

<bean id="localhostInetAddress" class="java.net.InetAddress" factory-method="getLocalHost" />
<bean id="hostname" factory-bean="localhostInetAddress" factory-method="getHostName" />

但在 Java 8 中,bean“主机名”现在包含字符串

localhost

在 Java 8 之前,它曾经包含在命令行上运行的“主机名”值,即

[myuser@machine1 ~]$ hostname
machine1.mydomain.org

如何重新配置​​我们的 bean 以便它获取命令行列出的主机名?我不想在任何地方硬编码。

最佳答案

来自 InetAddress java 8 is not getting the hostname :

There was similar bug fired in JDK.

What I understand is that they changed default resolution process.

They honor configuration in /etc/nsswitch.conf where hosts are configured for /etc/hosts that gives it main priority for name resolution.

Usually /etc/hosts has record for 127.0.0.1 localhost that provide name for host localhost

关于java - 在 Java 8 中,如何在我的环境中不对其进行硬编码来获取我的主机名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48325178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com