If you define multiple services of the same type in your Docker Compose yaml, you'll get multiple connection details beans of the same type in the context. Those beans are hard to inject as you can't just rely on the type. Matching by name is possible, but we don't document how the names are generated.

Some things that we could do:

  1. Document how the bean names are generated
  2. Support a label that allows the bean name to be specified
  3. Support a label that allows a connection details bean to be marked as @Primary, with @Qualifier, @Fallback, etc.

Comment From: dmfrey

+1

Comment From: nosan

Document how the bean names are generated

At the moment, the bean name is generated by using the following pattern:

connectionDetailsType in camelCase for containerName in camelCase

For example:

connectionDetailsType=RedisConnectionDetails
containerName=t-redis-1

the generated bean name is: redisConnectionDetailsForTRedis1

Support a label that allows the bean name to be specified

I'm not sure that's possible, as more than one ConnectionDetails is being created for a single Docker Compose service.

For example, for postgresql the following ConnectionDetails could be created:

  • r2dbcConnectionDetailsForTDatabase1
  • jdbcConnectionDetailsForTDatabase1
  • liquibaseConnectionDetailsForTDatabase1
  • flywayConnectionDetailsForTDatabase1

and using just bean name will lead to Invalid bean definition with name ...

I suggest bean-name-prefix instead of bean-name, so if a prefix is specified the name will be generated by the following pattern:

bean-name-prefix in camelCase + connectionDetailsType in camelCase

For example:

org.springframework.boot.bean.name-prefix=redis-container
connectionDetailsType=RedisConnectionDetails

The generated bean name will be redisContainerRedisConnectionDetails. Additionally, I suggest updating the current bean name generation to follow a bean-name-prefix pattern.

For example:

containerName=t-redis-1
connectionDetailsType=RedisConnectionDetails

the generated bean name will be tRedis1RedisConnectionDetails if a prefix is not specified.

Support a label that allows a connection details bean to be marked as @Primary, with @Qualifier, @Fallback, etc.

I suggest the following labels, as they affect how Bean Autowiring will be handled:

org.springframework.boot.bean.primary=true/false
org.springframework.boot.bean.fallback=true/false
org.springframework.boot.bean.default-candidate=true/false
org.springframework.boot.bean.autowire-candidate=true/false
org.springframework.boot.bean.qualifier=value

I've prototyped all these changes in my branch

If you're in favor of the proposed changes and would like to move forward with this enhancement, feel free to let me know, and I'll go ahead and create a PR.