I have some libraries written in java and using Spring Boot 2.6.4. Each one -for some reasons- has a database connections as In some cases I want that the libraries stores the data on a different database.
For this purpose, I generate multiples database configuration (one as an example):
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "lib1ManagerFactory", transactionManagerRef = "lib1TransactionManager", basePackages = {
"my.lib1.package"})
public class Lib1DatabaseConfigurator {
@Bean
@ConfigurationProperties(prefix = "spring.lib1.datasource")
public DataSource lib1DataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public LocalContainerEntityManagerFactoryBean lib1ManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("lib1DataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("my.lib1.package").build();
}
@Bean
public PlatformTransactionManager lib1TransactionManager(
@Qualifier("lib1ManagerFactory") EntityManagerFactory lib1EntityManagerFactory) {
return new JpaTransactionManager(lib1EntityManagerFactory);
}
}
Then, I have an application that has some entities and it connects to a database as any of the previous libraries (configuration very similar). The difference is that it is an application that executes as a rest server, but also some internal modules can be use as libraries for other applications (it is a maven multi-module application, where the business logic can be used as library, but also has other maven modules that execute the library as a rest application for accepting rest calls).
On this application, when I execute it, I get the error No qualifying bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder
. In all sources I have checked, this will be fixed if I include the @Primary
on the database configuration.
But as I want that database entities and configuration will be used as a library for some other applications, I will want to avoid the @Primary
annotation, as other applications will have its custom database configuration and probably with this annotation.
Note that I have some Custom Implementation of repositories in some cases, the type of:
@Repository
public class CustomLib1RepositoryImpl<T> implements CustomLib1Repository<T> {
@PersistenceContext
@Qualifier(value = "lib1ManagerFactory")
private EntityManager entityManager;
private Class<T> entityTypeClass;
...
}
What I want to achieve is: create a library that has its custom database configuration that I can use in multiples applications. Therefore the final application will have its own database configuration and the database configuration from the imported library.
but
In one specific case, the library can also run as an standalone application. That means that must have its own database configuration running, but this one can be exported when used as a library.
What I want to avoid also, is to not duplicate library's database configurations among applications (then include it on the libraries).
CodePudding user response:
I would create a small extra Maven module that acts as the standalone app and has the configuration for running the library standalone. This way, I suspect you can also move a bit of code out of the actual library, making it more lightweight.
CodePudding user response:
Seems that the way I am trying to do this is not correct. From the official documentation the @Primary
must be always present.
The only solution I can imagine currently for the problem then, will be change my idea of having one Datasource Configuration to create multiples different files. Without @Primary
the ones that will be on the maven modules that will be used a libraries later, and other ones with @Primary
where I need to run on the project (i.e. unitary testing and Rest Server module).