在我開始在我的資料源配置類中添加更多存盤庫和物體包之前,我的代碼一直可以正常使用單個存盤庫包。代碼失敗,例外:... Error creating bean with name 'waecChemistryObjRepo' defined in com.example.resource.akademiks.waec.obj.dao.WaecChemistryObjRepo defined in @EnableJpaRepositories declared on AkademiksDbConfig: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.example.resource.akademiks.waec.obj.entity.WaecChemistryObj
主資料源配置類:
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.example.resource.akademiks.waec.obj.dao",
"com.example.resource.akademiks.waec.theory.dao", "com.example.resource.akademiks.waec.theory.answer.dao"},
entityManagerFactoryRef = "akademiksEntityManagerFactory",
transactionManagerRef = "akademiksTransactionManager")
public class AkademiksDbConfig {
@Bean
@Primary
@ConfigurationProperties("spring.datasource.waecakademiks")
public DataSourceProperties akademiksDataSourceProperties() {
return new DataSourceProperties();
}
@Primary
@Bean
@ConfigurationProperties("spring.datasource.waecakademiks")
public DataSource akademiksDataSource() {
return akademiksDataSourceProperties().initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
}
@Primary
@Bean(name = "akademiksEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
akademiksEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(akademiksDataSource())
.packages("com.example.resource.akademiks.waec.obj.entity")
.packages("com.example.resource.akademiks.waec.theory.questions.entity")
.packages("com.example.resource.akademiks.waec.theory.answer.entity")
.build();
}
@Primary
@Bean
public PlatformTransactionManager akademiksTransactionManager(
@Qualifier("akademiksEntityManagerFactory")
LocalContainerEntityManagerFactoryBean akademiksEntityManagerFactory
) {
return new JpaTransactionManager(akademiksEntityManagerFactory.getObject());
}
}
存盤庫:
package com.example.resource.akademiks.waec.obj.dao;
@Repository
@RepositoryRestResource(collectionResourceRel = "chemistry", path = "chemistry")
public interface WaecChemistryObjRepo extends JpaRepository<WaecChemistryObj, Integer> {
服務層:
package com.example.resource.akademiks.waec.theory.chemistry.service;
public interface WaecChemistryTheoryService {}
服務實施:
@Service
public class WaecChemistryObjServiceImpl implements WaecChemistryObjService {
@Autowired
private WaecChemistryObjOptionRepo chemistryOptionRepo;
物體類:
package com.example.resource.akademiks.waec.obj.entity;
@Entity
@Table(name = "WaecChemistryObjAnswer")
public class WaecChemistryObjAnswer {
其他物體和存盤庫已被排除在外。
uj5u.com熱心網友回復:
該packages方法采用 varargs 引數:
EntityManagerFactoryBuilder.Builder packages(String... packagesToScan)
您呼叫它 3 次,而不是在一次呼叫中傳遞 3 個包。
更一般地說:如評論中所建議的那樣,利用自動配置并遵循配置方法的約定。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/399170.html
上一篇:我可以將控制臺輸出鏡像到網站嗎?
