Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Spring what is the difference between @Profile and @ActiveProfiles

Tags:

java

spring

What is the difference between using @Profile and @ActiveProfiles on a Spring Test configuration

@Configuration @EnableRetry @ActiveProfiles("unittest")  static class ContextConfiguration { 

and

@Configuration @EnableRetry @Profile("unittest")  static class ContextConfiguration { 
like image 618
kellyfj Avatar asked May 18 '17 19:05

kellyfj


People also ask

What is @profile in Java?

Profiling is the process of examining an application to locate memory or performance-related issues. When profiling a Java application, you can monitor the Java Virtual Machine (JVM) and obtain data about application performance, including method timing, object allocation and garbage collection.

What is the default 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.

On which of these can you annotate the @profile class method?

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

What is @activeprofiles in Spring Boot?

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. Meaning it is only supposed to be used to declare active Spring profiles for test classes.

What is the use of @activeprofiles annotation in Spring Integration Test?

@ActiveProfiles are used in Spring integration test as following. To Activate multiple profiles using @ActiveProfiles, specify profiles as an array. Here on this page we will create Spring integration test classes using @ActiveProfiles annotation with complete example.

How to use @profile feature in spring 4?

I have written a very simple example to demonstrate the power of @Profile feature in Spring 4. This example declares data source for dev and prod. By using the @Profile annotation, a suitable data source will be invoked by the test class. The syntax for this annotation is @Profile (“dev”).

How to get the list of active profiles programmatically in spring?

Get Active Profiles Spring's active profiles drive the behavior of the @Profile annotation for enabling/disabling beans. However, we may also wish to access the list of active profiles programmatically. We have two ways to do it, using Environment or spring.active.profile. 6.1. Using Environment


1 Answers

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

like image 123
Navpreet Singh Avatar answered Sep 20 '22 21:09

Navpreet Singh