Application Properties

This is Spring boot application built-in file created at the time of project creation using STS and initializr named as application.properties file.

Its default location is src/main/resources as mentioned in below screenshot.

This file can contain both system define and custom properties.

Below is an example to define property inside application.properties file.

Here, # denotes for writing a comment.

Create a class UserController.java file having below content in place.

@RestController annotation used to created RESTful web services which can be accessed using http methods like GET, POST, etc. and it is the combination of @Controller and @ResponseBody.

@RequestMapping annotation is used to define header, uri, method type, parameters, path, consume type, etc.

@Value(“${custom.practice.name}”) annotation will help us to access value of a property declared in application.properties file.

If property not declared or found inside application.properites file will throw below exception:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘spring.application.name’ in value “${custom.practice.name}”

To Resolve or suppress above exception, we can declare default value while accessing property in Java file like @Value(“${custom.practice.name:javadoubts}”)

Sometime, it doesn’t work with STS, eclipse and show below error.

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘spring.application.name’ in value “${custom.practice.name}”

Open command prompt and run mvn spring-boot:run command to start the Spring boot application and hit below URL

Hit below URL in browser will give below output:

Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.

0