Connecting to DataSource when using "exclude = HibernateJpaAutoConfiguration.class" [JAVA] [SPRING]

Viewed 40

I've been trying to make my application not automatically connect to its datasource as soon the spring context finishes initialization. That was done by adding

@SpringBootApplication(exclude = {
        HibernateJpaAutoConfiguration.class,
        JpaRepositoriesAutoConfiguration.class
    })

After doing it, application fails to start with the following error message:

Parameter 0 of method entityManagerFactory in Datasource required a bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' that could not be found.

That's how I connect to my datasource (h2, connection parameters are located in application.properties:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory", basePackages = {
        "com.app.main.repository.person" // note this is the folder that contains spring-data jpa interface
})
public class Datasource {

    @Primary
    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix = "database2.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
                                                                       @Qualifier("dataSource") DataSource dataSource) {
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("hibernate.hbm2ddl.auto", "update");
        properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        return builder.dataSource(dataSource).properties(properties)
                .packages(
                        "com.app.main.entities.domain.person" // entity folder which has entity class anotated with @Entity
                ).persistenceUnit("Persistence").build();
    }

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(
            @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}

As additional content, this should be helpful as well: Could not autowire. No beans of 'EntityManagerFactoryBuilder' type found.

Based on that, I have 2 questions:

1: Why the first parameter of my "entityManagerFactory" method (which is EntityManagerFactoryBuilder builder,) can't find "EntityManagerFactoryBuilder" bean anymore?

2: The final goal is to connect to the datasource not instantly as the application launches, but to connect to it after another process is done and then finally start database connection. In other words, is there any way to connect to my datasource after excluding its autoconfiguration classes? If yes, how?

Any assistance would be very welcomed, Thanks!

EDIT: Here's the call which is producing the error:

public static ConfigurableApplicationContext context;

public void initializeApplication() {
    Properties properties = new Properties();
    context = new SpringApplicationBuilder()
            .sources(Main.class)
            .properties(properties)
            .run(getParameters().getRaw().toArray(String[]::new));
}

Stacktrace:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' defined in class path ...

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

0 Answers
Related