3

I have created Profiles in Java class like this,

@Profile(value = "cache")
public class MapCache {
....
}

These profiles are not getting activated when spring.profiles.active used, but if i use spring.profiles.include profiles are working fine.

I would like activate profiles through properties which are added in application.properties

Note: application is running in independent jetty instance.

Any tip would be great on this.

Sunil Rk
  • 999
  • 6
  • 12
  • 35

4 Answers4

5

To activate a profile via annotations you need @ActiveProfile("profileName"). The @Profile annotation is used to label an object as being a member of the profile.

Rob Evans
  • 2,822
  • 1
  • 9
  • 15
  • Thanks for the reply, but I would like to activate via properties because I don't to modify java file when I want to add or delete active profiles – Sunil Rk Nov 13 '20 at 08:01
  • Add this into properties or set via an environment variable `spring.profiles.active=dev` - bear in mind you need to have the right filename when loading via a properties file. – Rob Evans Nov 13 '20 at 08:35
  • i am surprised why spring.profiles.active=cache is not working, cache is a profile created on class as shown above. I got what you want to saying, but my profiles are on the classes. – Sunil Rk Nov 13 '20 at 08:40
  • If you look at the top of the spring log output it usually tells you which profile has been activated (within the first 10 lines). If the default profile is loaded and you've added the line into config, try setting it as an environment variable. If it works its likely the properties file you've specified is not being picked up. Try it and update the question with any log output your seeing in relation to the active profile loaded – Rob Evans Nov 13 '20 at 08:42
4

you can also try to run the application and pass them as command line args

java -jar -Dspring.profiles.active={your_profile_name} application.jar

or if you run the app via maven:

mvn spring-boot:run -Dspring-boot.run.profiles={your_profile_name}

Here is an nice example I found on the internet with all the ways you can set the spring profile, it should help : https://www.baeldung.com/spring-profiles

Opri
  • 590
  • 2
  • 11
0

Hint: for IntellyJ fans
Do not forget to select "Add VM Options" in "Modify Options" dropdown of Edit Configurations dialog and then enter
-Dspring.profiles.active=...

ZoltanB
  • 79
  • 7
-1

I encountered a similar issue where SpringBoot wasn't activating the profiles set in the spring.profiles.active application property.

The issue was the result of the code base using a non standard name and location for the application property file (not my doing). Once I specified the location via the command line arg- --spring.config.location=/non/standard/location/acme.properties- the profile was activated.

JohnC
  • 698
  • 7
  • 12