尽管有一些相当清楚的示例,但我无法手动配置Spring Boot应用程序以使其正常工作。我通常允许它直接从application.properties文件进行配置,但是要求要求我使用多个数据源。而且,是的,我尝试按照此处看到的帖子的建议重新创建Baeldung示例(https://www.baeldung.com/spring-data-jpa-multiple-databases)。这是我的配置:
@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
public class CustomerRefreshService {
@Autowired
private ExistingLoanModelDataRepository existingLoanModelDataRepository;
public void execute() {
System.out.println("show me");
}
}
存储库:
@Repository
public interface ExistingLoanModelDataRepository extends CrudRepository<ExistingLoanModelData, Long> {
ExistingLoanModelData findByCustomerId(Long customerId);
}
错误:
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)}
...
我在做什么错?这几乎是直截了当的。为了简化起见,我消除了其他(mongo)配置设置。这必须是显而易见的...对我而言不是:-)
答案 0 :(得分:1)
看起来像错字。
@EnableJpaRepositories(basePackages =“ com.ezcorp.cost u merrefresh.db.sqlserver”,entityManagerFactoryRef =“ sqlserverEntityManagerFactory”,transactionManagerRef =“ sqlserverTransactionManager”)