Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the host name in spring configuration file?

Is there any easy way to get the host name in spring configuration file ? Currently I am using Java code to get the host name and and auto wire the property in the bean . But looking for less coding approach if any !

Thanks

like image 560
Gopi Avatar asked Dec 05 '15 17:12

Gopi


People also ask

How do I find my host and Spring boot port?

You can get this information via Environment for the port and the host you can obtain by using InternetAddress . Getting the port this way will only work, if a) the port is actually configured explicitly, and b) it is not set to 0 (meaning the servlet container will choose a random port on startup).

What is the name of Spring configuration file?

A Spring configuration file is an XML file that contains the classes information. It describes how those classes are configured as well as introduced to each other. The XML configuration files, however, are verbose and cleaner.

How do I change my hostname in Spring boot?

To change localhost to domain name in Spring Boot, you need to edit the application. properties file and change the server. address property to the domain name.

How does @configuration work in Spring?

Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.


1 Answers

The following will give you the hostname in java

return InetAddress.getLocalHost().getHostName();

where InetAddress belongs to the java.net package. You can add that to your java configuration file. If you want to do it in xml, you can do the following

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

<bean id="hostname"
    factory-bean="localhostInetAddress"
    factory-method="getHostName"/>
like image 105
Kabir Avatar answered Sep 28 '22 16:09

Kabir