In Spring what is the difference between @Profile and @ActiveProfiles
Any @Component or @Configuration can be marked with @Profile to limit when it is loaded.
You define @Profile
for your:
- classes -directly or indirectly- annotated with
@Component
, including@Configuration
classes - methods annotated with
@Bean
Then while testing, you choose which profile(s) you want by specifying them in @ActiveProfiles
.
ActiveProfiles is a class-level annotation that is used to declare which active bean definition profiles should be used when loading an ApplicationContext for test classes.
It has no effect if used outside of the test context.
Summary
You assign profile to your components with @Profile
; while testing select them with @ActiveProfiles
, while developing select them with spring.profiles.active
property.
@Profile
is used to define different @Bean
definitions for different contexts, e.g:
public class BeanConfiguration {
@Bean
@Profile({"local", "dev", "ci-dev", "homolog"})
public SomeHttpClientBean adyenClientFactorySandbox() {
return SomeHttpClientBean.builder()
.url("https://test.example.com")
.build();
}
@Bean
@Profile("prod")
public SomeHttpClientBean adyenClientFactorySandbox() {
return SomeHttpClientBean.builder()
.url("https://production.example.com")
.build();
}
}
Once you have that configuration, when starting your application, all you need to do is set which profile is active, either by spring.profiles.active
property or by annotating a class:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@ActiveProfiles("ci-dev")
public class SpringBootTestBase {
@Test
...
}
In short, @Profile
defines a profile like a Debug profile and a Production profile etc... However @ActiveProfiles
comes into picture in case of an ApplicationContext
and defines which profiles should be active if respective ApplicationContext
is being used.
As mentioned in JavaDoc of Spring official website:
@Profile
A profile is a named logical grouping that may be activated programmatically via ConfigurableEnvironment.setActiveProfiles(java.lang.String...) or declaratively by setting the spring.profiles.active property as a JVM system property, as an environment variable, or as a Servlet context parameter in web.xml for web applications. Profiles may also be activated declaratively in integration tests via the @ActiveProfiles annotation.
@ActiveProfiles
ActiveProfiles is a class-level annotation that is used to declare which active bean definition profiles should be used when loading an ApplicationContext for test classes.
Also, you can see here for more information about @Profile
Spring Profiles provide a way to segregate parts of your application configuration.
Any @Component
or @Configuration
can be marked with @Profile
to limit when it is loaded which means that component or configuration will be loaded in the application context only when the active profiles is same as the profile mapped to a component.
To mark a profile active, spring.profiles.active
property must be set in application.properties
or given as an VM argument as -Dspring.profiles.active=dev
While writing Junit, you would want to activate some profile so as to load the required configuration or Component. Same can be achieved by using @ActiveProfile
annotation.
Consider a configuration class which is mapped to profile dev
@Configuration
@Profile("dev")
public class DataSourceConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost/test");
ds.setUsername("root");
ds.setPassword("mnrpass");
return ds;
}
@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}
}
Consider a configuration class which is mapped to profile prod
@Configuration
@Profile("prod")
public class DataSourceConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:oracle://xxx.xxx.xx.xxx/prod");
ds.setUsername("dbuser");
ds.setPassword("prodPass123");
return ds;
}
@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}
}
So, if you want to run your junit test cases in dev
profile then you have to use the @ActiveProfile('dev')
annotation. This will load the DataSourceConfig bean defined in dev profile.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@ActiveProfiles("dev")
public class Tests{
// Junit Test cases will use the 'dev' profile DataSource Configuration
}
Conclusion
@Profile
is used to map a class to a profile
@ActiveProfile
is used to activate a particular profile(s) during junit test class execution