[英]why does my code throw a "bean not managed exception"?
在我開始在我的數據源配置 class 中添加更多的存儲庫和實體包之前,我的代碼僅在單個存儲庫 package 上運行良好。 代碼因異常而失敗: ... 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
... 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
主數據源配置 class:
@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;
實體 class:
package com.example.resource.akademiks.waec.obj.entity;
@Entity
@Table(name = "WaecChemistryObjAnswer")
public class WaecChemistryObjAnswer {
其他實體和存儲庫已被排除在外。
packages
方法采用 varargs 參數:
EntityManagerFactoryBuilder.Builder packages(String... packagesToScan)
您調用它 3 次,而不是在一次調用中傳遞 3 個包。
更一般地說:利用自動配置並遵循配置方法的約定,如評論中所建議的那樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.