@Profile : Profile specific beans
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Conditional(ProfileCondition.class)
public @interface Profile {
String[] value();
}
- When you wish to have specific bean in production and some in development or qa profile then Spring can create profile specific beans using the @Profile annotation.
- Spring takes this specific beans creation decision at runtime, not at a build time.
@Configuration
@Profile("dev")public class TestData{
}
This TestData configuration class will be used only for development profile.
@Configuration
public class Database{
@Bean
@Profile("prod")
public DataSource getOracleDataSource(){
/* This bean will be used for production profile only*/
}
@Bean
@Profile("dev")
public DataSource getMySqlDataSource(){
/* This bean will be used for development profile only*/
}
@Bean
@Profile("qa")
public DataSource getMySqlDataSource(){
/* This bean will be used for qa profile only*/
}
}
Also you can specify the profile for beans in the XML file
<beans profile="dev">
</beans>
<beans profile="qa">
</beans>
<beans profile="prod">
</beans>