2

Using spring boot yaml config, I have a datasource that looks like this:

datasource:
  url: jdbc:postgresql://somehost/somedb
  username: username
  password: password
  hikari:
    connection-timeout: 250
    maximum-pool-size: 1
    minimum-idle: 0

I can succesfully point to different DBs based on profile, but I'd like to setup a profile that does not use this datasource at all. When I use that profile, however, I get this:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.

Reason: Failed to determine a suitable driver class

How do I use this datasource in some profiles, but not in others?

IVR Avenger
  • 15,090
  • 13
  • 46
  • 57

2 Answers2

3

You can skip the bean for specific profiles using `@Profile("!dev") annotation

profile names can also be prefixed with a NOT operator e.g. “!dev” to exclude them from a profile

from docs here

If a given profile is prefixed with the NOT operator (!), the annotated component will be registered if the profile is not active — for example, given @Profile({"p1", "!p2"}), registration will occur if profile 'p1' is active or if profile 'p2' is not active.

Profiles can also be configured in XML – the tag has “profiles” attribute which takes comma separated values of the applicable profiles:here

 <beans profile="dev">
  <bean id="devDatasourceConfig"
  class="org.baeldung.profiles.DevDatasourceConfig" />
</beans>
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • This is helpful! The problem is that the Bean for the datasource is wrapped up in other classes; I'm not sure how to get down to the "foundation" where the @Bean is declared. – IVR Avenger Oct 04 '18 at 13:37
  • The datasource bean is on a dependency that I've pulled into my app, rather than in a class file I control. How would I set the @Profile annotation on a Bean that I don't "see"? – IVR Avenger Oct 04 '18 at 13:55
1

Change to:

spring: 
   datasource:
      url: jdbc:postgresql://somehost/somedb
      username: username
      password: password
      hikari:
         connection-timeout: 250
         maximum-pool-size: 1
         minimum-idle: 0

Springboot works with Autoconfiguration by default, but you can customize excluding some AutoConfiguration classes

Edit your configuration to skip AutoConfiguration:

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})

Make your own datasource by profile

@Bean
  @Profile("dev")
  DataSource dataSourceDevProfile(org.springframework.core.env.Environment environment) throws Exception {
     return DataSourceBuilder.create().url("").driverClassName("").password("").username("").build();
   }

@Bean
@Profile("!dev")
DataSource dataSourceNoDev(org.springframework.core.env.Environment environment) throws Exception {
    return DataSourceBuilder.create().url(environment.getProperty("spring.datasource.url")).driverClassName("").password(environment.getProperty("spring.datasource.password")).username(environment.getProperty("spring.datasource.username")).build();
}

Or Totally Programatically

@Bean
DataSource dataSource2(org.springframework.core.env.Environment environment) throws Exception {
    if (environment.acceptsProfiles("dev")){
        //return datasource dev
    }else{
        //return datasource prod
    }
jrey
  • 2,163
  • 1
  • 32
  • 48