Publish Your Article Here

Spring Boot – Profile based properties example.

A profile is a set of settings that can be configured to override settings from Each profile is contained in a file named where profilename is the name of the profile. Now, a profile could configure anything you want, however for most projects it is proposed to have the following profiles:

dev for your local development settings

staging for your staging server settings

prod for your production settings

test for running your tests

Whether you have all above environment in your application or not, you must have a dev, a prod and a test profile. The configuration for these environments needs to be different for obvious reasons. Now lets see how spring boot manages profile based properties in any application. All you need is create different properties for different profiles or environments as shown below:

Spring boot will bind all the properties to below class:

package com.java2novice.springboot.util;

import java.util.List;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.context.annotation.Configuration;

public class CmdbProperties {

        private String resourceUrl;

        private List<Integer> resourcePort;

        private Integer resourceCount;

        public String toString() {

                return "resourceUrl: "+ this.resourceUrl+"\n"
                                + "resourcePort: "+this.resourcePort+"\n"
                                + "resourceCount: "+this.resourceCount+"\n";

        public String getResourceUrl() {
                return resourceUrl;
        public void setResourceUrl(String resourceUrl) {
                this.resourceUrl = resourceUrl;
        public List<Integer> getResourcePort() {
                return resourcePort;
        public void setResourcePort(List<Integer> resourcePort) {
                this.resourcePort = resourcePort;

        public Integer getResourceCount() {
                return resourceCount;

        public void setResourceCount(Integer resourceCount) {
                this.resourceCount = resourceCount;

And here is the Spring boot application class:

package com.java2novice.springboot;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.java2novice.springboot.util.CmdbProperties;

public class SpringBootWebApplication {

        private static Logger logger = LoggerFactory.getLogger(SpringBootWebApplication.class);

        private CmdbProperties cmdbProperties;

        public static void main(String[] args) throws Exception {
      , args);

        public void init() {


Now package the application and run: You can pass the profile values as part of command line arguments by using "". We will pass prod profile as an input:

java -jar target/spring-boot-tutorials-0.0.1-SNAPSHOT.jar

resourcePort: [80, 443]
resourceCount: 2

java -jar target/spring-boot-tutorials-0.0.1-SNAPSHOT.jar
resourcePort: [8080]
resourceCount: 2
<< Previous Program | Next Program >>

Spring-Boot Examples

  1. Spring-Boot initial setup.
  2. Spring-Boot hello world example
  3. What is spring-boot-starter-parent in Spring-Boot pom.xml file?
  4. What is @SpringBootApplication annotation in spring boot?
  5. What is in spring boot?
  6. What is @ConfigurationProperties annotation in spring boot?
  7. Spring Boot @ConfigurationProperties example
  8. Spring Boot @ConfigurationProperties Property Validation
  9. Difference between @ConfigurationProperties and @Value
  10. Spring boot web application configurations.
  11. How to run spring boot application through command line?
  12. How to run spring boot as a standalone application (non-web)?
  13. Spring boot property resolution order.
  14. Spring Boot – Profile based properties example.
  15. How to configure logback (SLF4J) logging to spring boot applications?
  16. How to update application context path in spring boot?
  17. How to disable spring logo banner in spring boot?
  18. Spring Data JPA with Spring Boot Applications - Oracle - example
  19. Spring Data JPA with Spring Boot Applications - MySql example
  20. How to configure Spring Boot to show Hibernate SQL Query in logs?
  21. Spring Boot – List all Beans loaded in the ApplicationContext
  22. How to load external property files into Spring Boot application?
  23. How to rename file in Spring Boot application?
  24. How to configure multiple DataSources (Databases) with Spring Boot and Spring Data?
Knowledge Centre
Inner class and Anonymous class
Inner class: classes defined in other classes, including those defined in methods are called inner classes. An inner class can have any accessibility including private.

Anonymous class: Anonymous class is a class defined inside a method without a name and is instantiated and declared in the same place and cannot have explicit constructors.
Famous Quotations
We know what we are, but know not what we may be.
-- William Shakespeare

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2024 by Nataraja Gootooru. All Rights Reserved.