[英]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.