0

I am facing an issue with Spring Boot 3 native compilation where the project contains two JpaRepository connecting to two different datasources. The creation of the second datasource configuration depends on the first datasource and JpaRepository as it contains the details about the databases to connect.

The problem is that the Spring Boot Maven plugin process-aot goal does not generate bean definition for repositories which are processed later on. As a result, the application fails to start with the -Dspring.aot.enabled=true property enabled.

I have tried several solutions, including:

Adding the @DependsOn annotation to the second datasource configuration class, but it didn't work.

Adding the @DependsOn annotation to the second JpaRepository, but it also didn't work.

Adding a @Configuration class that contains both datasource configurations, but it also didn't work.

Here is a simplified version of my code:

package com.company.multidatabases.config

@Configuration
public class DataSource1Config {
    // datasource1 configuration
  @Autowired
  private MyEntity1Repository repo;

  private Map<Object,Object> dataSources;
}

package com.company.config

@Configuration
public class DataSource2Config {

    @Autowired
    private DataSource1Config dataSource1Config;

    @Bean
    public DataSource dataSource(){
    return // AbstractDataSourceRouting with datasources map from DataSource1Config
}



    // datasource2 configuration that depends on dataSource1Config
}

package com.company.multidatabases.repository
@Repository
public interface MyEntity1Repository extends JpaRepository<MyEntity1, Long> {
    // MyEntity1Repository definition
}

package com.company.repository
@Repository
public interface MyEntity2Repository extends JpaRepository<MyEntity2, Long> {
    // MyEntity2Repository definition that depends on DataSource2Config
}
And here is the error message I get:


Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myEntity1Repository' available

Any help or suggestion is highly appreciated. Thank you in advance.

Yash garg
  • 53
  • 1
  • 7
  • Did you try adding `@EnableJpaRepositories("com.company.config.repository")` on your main class..? I recently converted my app to springboot 3 and faced same issue(not scanning repos) but this did the trick – Shankar Yadav Aug 21 '23 at 11:53

0 Answers0