🍃🎓 SPRING CERTIFICATION QUESTION: How to activate a Spring profile in production?
🍃🎓 SPRING CERTIFICATION QUESTION: How to activate a Spring profile in production?
* pass this system property: '-Dspring.profiles.active=embedded,jpa'
* do 'System.setProperty("spring.profiles.active", "embedded,jpa");' before creating the application context
* do 'System.setProperty("spring.profiles.active", "embedded,jpa");' after creating the application context
* use @ActiveProfiles
#spring #certificationquestion #vcp
Ways to Activate Profiles
* Profiles must be activated at run-time
- System property via command-line:
-Dspring.profiles.active=embedded,jpa
- System property programmatically
System.setProperty("spring.profiles.active", "embedded,jpa");
SpringApplication.run(AppConfig.class);
- Integration Test only: @ActiveProfiles
How can we activate a profile?
We have to do it at runtime.
One way to do it is by using a system property.
We could pass this -Dspring.profile.active and pass one or more than one profile.
In that case, we are activating the embedded and jpa profile.
If you want to do it programmatically, you'll have to do it before creating the application context.
That's before calling the spring application run method, we set the profile spring.profiles.active.
We define the profiles we want to activate and then we start the application context.
Another way to activate a profile and that's going to be valid for the test environment is by using the @ActiveProfiles.
So this cannot be used in production for instance.
This is only used in a test environment, in a JUnit test for instance where we want to create the application context.
We'll focus on this annotation in a later module, but don't get confused: this is not an annotation to activate a profile in your application.
It is just for the test environment.