Spring Profile annotation example

In this tutorial, we will see about Spring @Profile annotation. This tutorial is an extension to Spring boot profile example.

Profile annotation represents named logical grouping  may be activated via

  • By using ConfigurableEnvironment.setActiveProfiles(java.lang.String...)
  • By setting the spring.profiles.active property as a JVM system property, as an environment variable, or as a Servlet context parameter in web.xml for web applications.
  • You can also use @ActiveProfiles< while doing integration testing

The @Profile annotation may be used in any of the following ways:

  • As a type-level annotation on any class directly or indirectly annotated with @Component, including @Configuration classes
  • As a meta-annotation, for the reason of composing custom stereotype annotations
  • It can be used method-level annotation on any @Bean annotation

Let’s see with the help of an example.
Step 1: Go to "https://start.spring.io/" and create spring boot projects as per below screenshot.

Spring profile Initializr

Step 2: Import the maven project in eclipse.

step 3: Create a package named "org.arpit.java2blog.model"
create a model class named "Customer.java"

Step 4:Create a package named “org.arpit.java2blog.config”
create a config class named “ConfigurationDatabase.java”

Step 5: Create test class named “SpringProfileAnnotationDevTest” in src/test/java.


@SpringBootTest annotation is used to run Spring boot test.

@ContextConfiguration provides class-level metadata to determine how to load and configure an ApplicationContext for integration tests.

@ActiveProfiles is used to provide name of the profile which will be activated while running integration tests.

Step 6: Run the test
When you run above test, you will get below output:

Spring profile dev result

Step 7:Create another test class named “SpringProfileAnnotationProdTest” in src/test/java.
create a class named “SpringBootHelloWorldApplication.java”

Step 8: Run the test
When you run above test, you will get below output:
Spring profile prod result
As you can see, we are getting different bean based on active profile.

As we you can see we are getting name as “John” with @ActiveProfiles(value="dev") and “Martin in case of @ActiveProfiles(value="prod")

Project Structure

Spring Profile annotation

Source code

That’s all Spring profile annotation.

Was this post helpful?


You may also like:

Related Posts

  • 11 January

    Spring boot JDBC example

    Table of ContentsTools usedProject StructureCreate new Spring boot projectMaven configurationConfigure datasource in Application.propertiesCreate database tableCreate model classCreate Repository interface and implementationCreate service interface and implementationCreate Application classRun the applicationVerify database outputGithub Source code In this post, we will see Spring boot JDBC example. As we already know Spring boot does lot of auto configurations which […]

  • 02 January

    Spring boot devtools tutorial

    Table of ContentsOverviewJRebelIntroduction to Spring Boot DevToolsAdding Devtools to your projectSpring Boot DevTools FeaturesProperty DefaultsLoggingCachingAutomatic RestartLive ReloadGlobal SettingsRemote ApplicationsFile System WatchersConclusion In this post, we’ll be looking up what does spring has to offer for developer experience. Overview Using IDEs like Eclipse, Spring Tool Suite, Intellij Idea a typical development setup includes an application code […]

  • 26 December

    Spring boot H2 Database example

    Table of ContentsWhat is H2 database?Tools usedGithub Source code:Project StructureCreate new Spring boot projectMaven configurationH2 database configurationOverride H2 databse defaultsModelRepositoryServiceControllerSpring boot main fileRun the applicationTest the applicationCreate movie objectGet all movie objectsDelete movie id 3 In this post, we are going to see how to create Spring boot application integrating with H2 database. What is […]

  • 22 December

    Spring Boot CrudRepository

    Table of ContentsTools usedProject Structure:Wiring up MongoDBThe Repository ClassThe Save methodThe User Entity ClassBringing the UserDao and CrudRepository togetherServiceControllerApplication classRunning the code sampleConclusionSource code In this post, we’ll be looking up how to create and use Spring Boot CrudRepository. SpringBoot CrudRepository provides sophisticated CRUD functionality for the type of entity you want to be managed.This […]

  • 10 April

    Difference between Spring and Spring boot

    Table of ContentsSpring frameworkSpring bootAdvantages of Spring BootSpring vs Spring boot In this post, we will see difference between Spring and Spring boot. If you have followed spring project over time, you might have noticed it has become increasingly complex. If you want to create a spring application, you still need to put a lot […]

  • 17 September

    Spring Boot SOAP Web service Example

    Table of ContentsTools usedProject structureAdd wsdl4j dependencyXSD file to define domainGenerate domain classes based on XSDCreate Book repository with dummy dataDefine Book service endpointDefine web service configurationRun the applicatonTest WSDL URLTest Spring soap web serviceSource code In this post, we will see how to create soap web services with Spring boot. We will create contract […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.