0

I'm new to spring and I'm studying it. And stumbled upon the @Profile annotation.

I want to write a simple project with Spring (not Springboot) to learn how to load properties based on the environment using @profile annotation. Almost everywhere, the examples (Ex1, Ex2) I see only with the Springboot. I'm wondering whether we cannot write a Spring application that can dynamically load the properties based on the environment (dev, prod).

  1. Some examples ( Ex3, Ex4, Ex5) show with the @Profile but those have hardcoded the bean details for each environment like below. Is this how we have to write the property loading?

    @Profile("dev")
    @Bean
    public String devDBCcnnection() {        
    System.out.println(dbConfiguration.getUrl());    
    return "DB Connection for Dev";
    }
    
    @Profile("test")
    @Bean
    public String devTestCcnnection() {  
     System.out.println(dbConfiguration.getDriverClassName());
     return "DB Connection for Test";
    }
    
    @Profile("prod")
    @Bean
    public String devProdCcnnection() {
    System.out.println("DB Connection for Prod");
    return "DB Connection for Prod";
    }
    
  2. It has to write a bean for each profile like in the above example?

Can someone tell me using @Profiles, can't dynamically load the property values like in Spring applications?

Appreciate it if you can give the samples with Spring 5

ever alian
  • 1,028
  • 3
  • 15
  • 45
  • do you have multiple application properties profiles or only one file? If you use one file you can set those properties in your pom (maven) file. – DaviM Mar 22 '21 at 18:35

1 Answers1

0

Almost everywhere, the examples (Ex1, Ex2) I see only with the Springboot. I'm wondering whether we cannot write a Spring application that can dynamically load the properties based on the environment (dev, prod).

Spring boot uses the spring context. The spring context allows you to use profiles. Therefore no problem using profiles with simple Spring project (non spring-boot).

There are many ways that you can use Profiles.

One of them is the example that you gave with specific beans that have @Profile and get registered in spring for a specific profile.

Another one, more commonly used in enteprise applications is to ship a jar application with multiple application.yaml files. So for example you ship your application, containing dev-application.yaml and qa-application.yaml. You can then start your application selecting a specific profile to be active. Then that specific application.yaml will be used when the application starts up to build the spring context. So the aplication will be started with qa-application.yaml and will have a connection to the QA database.

But be careful the default application.yaml will also be loaded. The specific application.yaml for example qa-application.yaml will be loaded on top of default application.yaml.

The following article contains very good information about spring profiles spring profiles article

Considering my example here, I quotte something relevant from that article.

The Default Profile The default profile is always active. Spring Boot loads all properties in application.yml into the default profile. We could rename the configuration file to application-default.yml and it would work the same.

Other profiles will always be evaluated on top of the default profile. This means that if a property is defined in the default profile, but not in the qa profile, the property value will be populated from the default profile. This is very handy for defining default values that are valid across all profiles.

In order to activate a specific profile

For non spring-boot projects here is a very good answer spring active profile

For spring-boot projects you can

  1. Use a system variable to start your jar file

    java -Dspring.profiles.active=qa -jar myApp.jar

  2. Use an environment property to start your jar file

    export SPRING_PROFILES_ACTIVE=qa

    java -jar myApp.jar

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47