7

We have several DB connections in our application and therefore several configs for JPA. The configs only differ in things like schema name, DB hostname etc. The rest like hibernate setup etc. is (normally) the same. This results in multiple HibernateJpaVendorAdapter, data source etc. beans. They all need different names so that they don't collide. We currently set this up manually like:

@Configuration
@Bean
public class FooDbConfig {
     public DataSource fooDataSource() {
         return ...;
     }
     // ... more beans like HibernateJpaVendorAdapter etc.
} 

@Configuration
@Bean
public class BarDbConfig {
     public DataSource barDataSource() {
         return ...;
     }
     // ... more beans like HibernateJpaVendorAdapter etc.
} 

This of course is very fragile to maintain.

We'd like to have some kind of java config "Configurer" that creates such a setup with the necessary beans by passing a bean name prefix. It should then create all the necessary beans (data source, etc.) with a distinct name, prefixed by the given prefix (e.g. "fooDataSource" and "barDataSource").

What would be a nice approach to do this?

How could I generate bean aliases programmatically with java config?

James
  • 11,654
  • 6
  • 52
  • 81
  • 1
    If you are using spring 4.X Is possible doing using a BeanDefinitionRegistryPostProcessor, however i do not recommend because come with a lot of complications. Here is a sample of dynamic bean definition https://blog.jdriven.com/2015/04/spicy-spring-dynamically-create-your-own-beandefinition/ – nekperu15739 Mar 28 '19 at 10:52

0 Answers0