Travis
Travis

Reputation: 223

Spring boot manual config not scanning Repository objects?

Despite some fairly clear examples, I'm failing to get a manual configuration of a spring boot application to work. I usually allow it to configure right from the application.properties file but the requirement demands that I use multiple data sources. And, yes, I have tried to recreate the Baeldung example (https://www.baeldung.com/spring-data-jpa-multiple-databases) as recommended by the posts I've seen here. Here is my config:

@Configuration
@EnableJpaRepositories(basePackages = "com.ezcorp.costumerrefresh.db.sqlserver", entityManagerFactoryRef = "sqlserverEntityManagerFactory", transactionManagerRef = "sqlserverTransactionManager")
@EnableTransactionManagement
public class SqlServerConfig {

    @Bean(name="sqlserverDataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    @Primary
    public DataSource sqlserverDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        dataSource.setUrl("jdbc:sqlserver://aswn-tbd-db01:1433;databaseName=DE_RTDS");
        dataSource.setUsername("cs_user");
        dataSource.setPassword("cs_user");
        return dataSource;
    }

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean sqlserverEntityManagerFactory(final EntityManagerFactoryBuilder builder) {
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("hibernate.dialect", "org.hibernate.dialect.SQLServer2008Dialect");
        return builder
            .dataSource(sqlserverDataSource())
            .properties(properties)
            .packages("com.ezcorp.costumerrefresh.domain.sqlserver")
            .persistenceUnit("adminPersistenceUnit")
            .build();
    }

    @Bean
    @Primary
    public JpaTransactionManager sqlserverTransactionManager(@Qualifier("sqlserverEntityManagerFactory") final EntityManagerFactory factory) {
        return new JpaTransactionManager(factory);
    }

}

Service:

@Service
public class CustomerRefreshService {
    @Autowired
    private ExistingLoanModelDataRepository existingLoanModelDataRepository;

    public void execute() {
        System.out.println("show me");
    }
}

Repository:

@Repository
public interface ExistingLoanModelDataRepository extends CrudRepository<ExistingLoanModelData, Long> {
    ExistingLoanModelData findByCustomerId(Long customerId);
}

error:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ezcorp.customerrefresh.db.sqlserver.ExistingLoanModelDataRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    ...

What am I doing wrong? This is about as straight-forward as it gets. I've eliminated the other (mongo) config setup to simplify. This has to be an obvious one...just not to me :-)

Upvotes: 0

Views: 210

Answers (1)

dexter125
dexter125

Reputation: 21

Looks like a typo.

@EnableJpaRepositories(basePackages = "com.ezcorp.costumerrefresh.db.sqlserver", entityManagerFactoryRef = "sqlserverEntityManagerFactory", transactionManagerRef = "sqlserverTransactionManager")

Upvotes: 1

Related Questions