Rename application.properties file in Spring Boot Application

1. Overview

By default, our Spring Boot application loads the external properties from the application properties file. This is normally available in our project resources folder. We can modify the name of application properties file, if required, even though it is a good convention to follow.

In this article, we’ll review various options to load the external properties file with a different name.

2. Setup

Once we create a Spring Boot project,

  • Let’s delete the application.properties file from the resources folder of our project.
  • Then, let’s create a customapp.properties file in the resources folder of our project. We’ll provide all our application configurations in this properties file.

spring.config.name property holds the name of our properties file in the Spring Boot application.

Therefore, we’ll set our properties file as the value for this property using various options.

3. Passing Property during Startup

First, let’s pass our properties file name as an argument to the spring.config.name property while starting the application:

java -jar SpringBootRenameAppProperties-0.0.1-SNAPSHOT.jar --spring.config.name=customapp

4. Environment Variable

In this section, let’s provide the new properties file name to the SPRING_CONFIG_NAME environment variable. Then, we can start the application from the command line:

set SPRING_CONFIG_NAME= customapp
java -jar SpringBootRenameAppProperties-0.0.1-SNAPSHOT.jar

5. System Property

Next, let’s set the configuration file name as the System Property before starting our Spring Boot application.

We’ll use spring.config.name as property key and our configuration file name as the value:

public static void main(String[] args) {
    System.setProperty("spring.config.name", "customapp");
    SpringApplication.run(SpringBootRenameAppPropertiesApplication.class, args);
}

6. Application Property

Finally, let’s pass our configuration file name as a value to spring.config.name property while creating our Spring Boot application:

public static void main(String[] args) {
    new SpringApplicationBuilder(SpringBootRenameAppPropertiesApplication.class)
	.properties("spring.config.name:customapp")
	.build()
	.run(args);
}

7. Conclusion

We reviewed various way to rename application properties file of our Spring Boot project. We can choose any one of these options to rename the file. Just note that, in all the cases we never added .properties to the file name. The framework appends it automatically to end of the file name. So if we pass customapp.properties as properties file name, then the application will be searching for customapp.properties.properties.

The code examples from this article are available on GitHub.

Creating a New Spring Boot Project

1. Overview

In this article, we’ll discuss various ways to create a new Spring Boot project.

2. Using Spring STS

First, let’s create a Spring Boot project using STS(Spring Tool Suite).

STS is available for eclipse, Visual Studio and Atom IDE’s. We can download the latest version of STS from the Spring Tools site.

Let’s use eclipse flavor of STS in our article.

In STS, select File –> New –> Spring Starter Project:

Let’s provide our project information in the New Spring Starter Project screen:

The configurations available in this screen are :

  • Service URL — The spring boot project will be downloaded from this url. We don’t need to modify this since the Spring Boot templates are available here.
  • Name — name of our Spring Boot project
  • Type — indicates whether our project is a Maven or Gradle project
  • Packaging — configure if build will create a jar or a war
  • Java Version — the version of java to use for our project
  • Language — indicates which jvm language to use. The possible options are Java, Groovy and Kotlin
  • Group — the group id for our project
  • Artifact — artifact id of our project
  • Version — project version
  • Description — some information about our project
  • Package — the name of the root package

After configuring the properties, let’s click on Next to navigate to the New Spring Starter Project Dependencies screen:

The configurations available in this screen are:

  • Spring Boot Version — we’ll select the version of Spring Boot from the list of supported versions.

Next, let’s select the required dependencies from the list of available dependencies.

Finally, click on Next to complete providing the properties for our Spring Boot project.

We don’t need to provide any information in the next screen. Let’s just click on Finish to create our new Spring Boot project.

3. Using Spring Initializr

In this section, let’s create a new Spring Boot project using Initializr. This is particularly useful when STS installation is not available.

First, let’s navigate to the Spring Initializr site:

Similar to project creation on STS, this page has several configurable options:

  • Project — We can select either Maven or Gradle project
  • Language — In this option, we can select the programming language we’ll use in the project
  • Spring Boot — This section displays the list of supported Spring Boot versions. Let’s select our preferred Spring Boot version.
  • Group — we’ll provide Maven group id
  • Artifact — let’s provide artifact id in this field

Let’s click on More Options to provide more information on the project we are going to create:

  • Name — name of our Spring Boot project
  • Description — information about our project
  • Package Name — name of the root package of our project
  • Packaging — building Spring Boot projects can create either a jar or a war as a deployment artifact.
  • Java Version — select the java version for out Spring Boot project

Next, let’s select the dependencies needed for our project. After selecting the dependencies, let’s click on Generate Project:

This starts a zip file download. We’ll unzip the file and import the project into our IDE.

4. Starting Spring Boot Application

In the previous sections, we created a new Spring Boot project. In this section, let’s see how to run our new Spring Boot application in STS.

Right click on our project and select Run As –> Spring Boot App :

And, we’ll see messages similar to the below in the console indicating that our Spring Boot app is started:


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

2019-03-11 04:27:56.700  INFO 38676 --- [           main] c.p.s.SpringBootDemosApplication         : Starting SpringBootDemosApplication on ProgramSquare with PID 38676 (D:\programsquare\tutorials\SpringBootDemos\target\classes started by kiran in D:\programsquare\tutorials\SpringBootDemos)

5. Conclusion

In this article, we discussed various options to create a Spring Boot project. Then, we started the project from STS. We can use any one of the options to create a new Spring Boot project.

Change Server Port in Spring Boot

1. Overview

Typically, the Spring Boot apps run on the port 8080. However, in some cases we may need to run the apps on other ports. We can change the server port easily in Spring Boot.

2. Using Properties

Let’s change the server port using server.port property in our application properties:

server.port = 9000

In case, we are using yml, then we can change the port in application.yml properties file using server port property:

server:
  port: 9001

3. Using Command Line

We can also configure the port while launching the app. In this case, we don’t need to modify the application properties or the code.

Now, let’s provide the server port while launching the application using the cmd:

java -Dserver.port=9001 -jar SpringBootDemo.jar

Or, we can pass server port as an argument to the Spring boot application:

java -jar SpringBootDemo.jar –-server.port=9001

4. Changing Port Programmatically

Now, let’s change the server port programmatically by extending WebServerFactoryCustomizer interface:

@Component
public class ServerCustomization implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory > {

	public void customize(ConfigurableServletWebServerFactory server) {
		server.setPort(9002);
	}
}

5. Conclusion

In this article, we discussed various options to change the server port for Spring Boot apps.

As always the code for all these examples is available on GitHub.