Configuration and Properties : Spring Boot : Part 1

Kumar Pallav's photo
Kumar Pallav
·Oct 15, 2018·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Spring Boot as said is having an opinionated approach. It comes up with range of pre configuration options. Once we initialize application we have file available in our application. Since is already at classpath , Spring Boot allows you to use it for any configuration, be it yours or overriding existing spring boot configuration.

Let’s see one simple example , In our last chapter we created a sample spring boot application. It has a embedded tomcat server. Spring Boot has an opinion that tomcat server should use 8080 as default port. In reality it will not be always possible to run our application on 8080 , there could be cases where 8080 is already occupied by some other application or we by some reason want it to run on some port other than 8080. We have file which we can make use of , spring boot provides a property called “server.port” if added to file will override Spring boot opinion of 8080.

Let’s add server.port in file.


Once you add above property in spring boot application properties and server starts, you can see it running at 8081 now. In above figure you can also see , Spring Tool Suite suggesting extra possible value. These are defaults which come with spring boot application and spring has given us the chance to override these defaults. We can find the list of all defaults at

We can simply configure from server port to database settings (like dataSource) by adding values in We can add even custom property and use it. We will be using many of the available properties as we go along this book.

Let us also have a look on how can we use further and can get the values from properties file to our java classes.


As we have seen that we can define our properties at , there should be a easy way to get this values at our java files. @Value is the annotation to help us.

Let’s see how can we use it. Let’s define one property , this is a default Spring boot property ( already given by Spring Boot) , we will try to access it in our controller using @Value annotation.

We will add the line below at file

Once added let’s add a string named applicationName to our controller , We will also also add a method to access it , say applicationName which will only return us the application name.We will use existing UserController to test, Add following code snippet insite UserController.

private String applicationName;
protected String getApplicationName() {
return applicationName;

In above code we are not initializing applicationName variable , yet we will be able to get the value which we have added in via @Value annotation.

If we hit URL we will get the value for bound to applicationName variable

Share this