Search Tutorials

Understand Spring Boot + Profiles using simple example | JavaInUse

Spring Boot + Profiles

In a previous post we developed Spring Boot + Swagger2 documentation. In this post we add Spring profile to this implementation. Also we had previously implemented Spring Profiles using Spring MVC. Spring Profiles allows users to register beans depending on the profile(dev, test, prod etc). So when the application is running in DEVELOPMENT only certain beans can be loaded and when in PRODUCTION certain other beans can be loaded. Suppose our requirement is that the Swagger documentation be enabled only for the QA environment and disabled for all others. This can be done using Profiles. Spring Boot makes using Profiles very easy.

Spring Boot Swagger- Table of Contents

Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot + Swagger + Profile - Implementing Spring Boot Profile for a Swagger application Spring Boot + Swagger 3 (OpenAPI 3) Hello World Example


This tutorial is explained in the below Youtube Video.

Lets Begin-

So the Spring Boot Swagger project will be the starting point for this implementation.
The project will be as follows-
Spring Boot Profile Tutorial
We will define the Spring Profile for the Swagger implementation class SwaggerConfig such that it will get loaded only when the deployment is for QA else it will be disabled. For this in the file define the active profiles as follows- 

The SwaggerConfig class annoatate with the Profile tag as follows-
package com.javainuse.swaggertest;

import static;
import static;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


public class SwaggerConfig {

	public Docket postsApi() {
		return new Docket(DocumentationType.SWAGGER_2).groupName("public-api")

	private Predicate postPaths() {
		return or(regex("/api/posts.*"), regex("/api/javainuse.*"));

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("JavaInUse API")
				.description("JavaInUse API reference for developers")
				.contact("").license("JavaInUse License")

Currently if the application is deployed then the swagger will be disabled. Go to http://localhost:8080/swagger-ui.html.

Spring Boot Profiles Example

If the application is to be deployed in QA then change the profile in as follows- 
Now the swagger will be enabled after deployment. Go to http://localhost:8080/swagger-ui.html.

Spring Boot Swagger Tutorial
So using Spring Profiles we can enable or disable the loading of any bean.

Download Source Code

Download it -
Spring Boot + Swagger + Profiles

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions