Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set active profile via annotation in spring?

Tags:

java

spring

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)

like image 393
Malahov Avatar asked Jun 26 '13 08:06

Malahov


People also ask

What is Spring profile annotation?

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 ...

Can you use @component together with profile?

Yes, @Profile annotation can be used together with @Component on top of the class representing spring bean.

What is the default active profile in Spring boot?

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.


1 Answers

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.

like image 176
Jonathan Avatar answered Sep 28 '22 04:09

Jonathan