How can I set active profile via annotation in spring ?
For example:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ApplicationConfig.class }, loader = AnnotationConfigContextLoader.class)
@ActiveProfiles( profiles = {ApplicationProfiles.TEST} )
public class CacheManagerTest {
...
}
For JUnit test this works perfect, but how can I init production application context ? (I do not have any main method/сlasses)
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 purpose of composing custom stereotype annotations. as a method-level annotation on any @Bean ...
Yes, @Profile annotation can be used together with @Component on top of the class representing spring bean.
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.
You can pass in the active profile(s) at runtime using the spring.profiles.active
property:
-Dspring.profiles.active="profile1,profile2"
See the SpringSource blog post on the introduction of profiles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With