EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot application.properties
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Spring Boot application.properties

Spring Boot application.properties

Definition of Spring Boot application.properties

In Spring boot application, we have application properties file which is used to write the application-related property into that file. This file contains the different configuration which is required to run the application in a different environment, and each environment will have a different property defined by it. Inside the application properties file, we define every type of property like database, toke, different URL if we are using any interrogation within our application, and many more. We can use this value into the program also if we want by using the annotation provide by spring boot. In the coming section of the tutorial, we will see the internal working and its implementation in detail in order to use this while designing our application.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

As we have discussed that this file contains a different property for different configuration, let’s take a closer look at the syntax of how we can write these properties inside the file for beginners to understand it better see below;

spring.application.name = name_applcation

In this way we can define the name of our application, this represents the property as key-value pair here, every key associated with a value also. Let’s take a closer look at the practice syntax for this to get a better idea for this see below;

e.g. :

spring. application.name = demoservice

As you can see in the above property that we have define the name of our application by this property i.e. spring. application. name we have severed or we can say ‘Number of property to make use of. In the coming section, we will see how we can create this file inside our program and configure the required changes for our application.

How Spring boot application.properties works?

As of now we already know that the application.properties file contains the property for the application which helps the application to run in the different environment if we have any. Spring boot provides us standard to create the application file for our different environment where we can mention the different property for each of our environment. This file contains the value as key-value pair, where we have a key for each configuration that we want to make for the application. There is no limit for these property inside this file, we can mention ‘N’ numbers of configuration inside our file. You ca see, when we cared the spring boot project from any web tool from scratch this file get automatically added at the class path, and if you setting up the project manually then you have to create one application.properties file at the class path only, with the same name. In this section we will see how we can create it a what are the different properties we have to mentioned inside this file let’s get started;

1) The first step we need to make the application file for every environment we have. In spring boot we have to follow some standard given by it, we can create the application file in the below format;

application-{profile}.properties

As you can see you can create the application file like above, in the profile part you can mention the name of the profile you want.

2) Inside the application file we can give the name for the profile by using the key and properties defined by the spring boot application. Below see the syntax how we can do this in our application see below;

e.g. :

spring:
application:
name: DemoProfiles
profile: local

3) For the name part of the application properties file we can have this in place see below;

a) application-local. properties: This properties file will be responsible to add the set of the environments for local.
b) application-dev.properties: This properties file will be responsible to add the set of the environments for development.
c) application-prod.properties: This properties file will be responsible to add the set environment for production.
d) application-test.properties: This properties file will be responsible to add the set of the environments for test.
In this way, we can create the application property for the different environments we have.
Let’s look at some of the properties now;

a) server.port = 8080

This property is used to set the port for our application, here we have set the 8080 as the port on which our application will run, we can specify any port but it should we different in every service if we have multiple.

b) spring.application.name = demo-application

This property is used to give any meaningful name to the application, we can specify any name and it should be unique in case we have multiple service in our project.

c) spring.config.name: This will tell the config name

d) spring. config.location: This will tell the config file location

e) spring.info.build.location: Will tell the location to the build file

f) spring.info.build.encoding: Tell the build encoding

g) spring.info.git.encoding: File encoding

h) spring.jmx.default-domain: Will tell the JMX default domain

i) spring.jmx.server: JMX server name

j) spring. main.banner-model: Tell the mode to display the banner

Now comes the part when we want to access this vales inside the class of our program let’s take a look;
To access this we can use the @Value annotation from the spring boot framework, which will give us the value for the key from the property file. Let’s take a look at the syntax for this see below;

e.g. :

@Value("${property_key_name:default_value}")

In this way, we can access the value from the application properties file in spring boot framework. Also, we can use this value inside the program to access resources. Let’s get started with steps needed to perform this ;

1) First we have to make one project if you do not have already, follow the below URL to get the zip of the project and extract it into the editor after filling all the required fields there.

URL:

https://start.spring.io/

Spring Boot application.properties 2

2) we check for the application.properties file at the class path if you cannot find it then create a file at the class path with the same name. For reference, I am attaching one screenshot where to create it if not found.

Output:

Spring Boot application.properties 3

3) If you found it already then it must be blank without contain any configuration, now make some changes to make it work see ;

e.g. :

server.port = 8081
spring. application.name = demo-application

Now our application will run on the 8081 port, not on the default port remember.

4) check the main class of the application it should contain the below code to run the application. check it once.

e.g. :

package com.test.Traders;
import com.test.Traders.entity.Student;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.*;
@SpringBootApplication
public class TradersApplication {
public static void main(String[] args)
{
SpringApplication.run(TradersApplication.class, args);
}
}

5) Now if you run the application you will see the below output on the screen;

Output:

Spring Boot application.properties 4

Conclusion

By the use of it, we can make the required configuration which is required to make our application dev and prod ready. All the changes that we have made it easy to implement and do not require any heavy changes before using it, it is quite easy to use and handle as well by the developers.

Recommended Articles

This is a guide to the Spring Boot application. properties. Here we discuss the definition, syntax, How the Spring boot application.properties works? examples with code implementation. You may also have a look at the following articles to learn more –

  1. Spring Boot DevTools
  2. Spring AOP
  3. Spring Hibernate Integration
  4. Spring Architecture
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more