为什么我的代码会抛出 "bean not managed exception"?
why does my code throw a "bean not managed exception"?
在我开始在我的数据源配置中添加更多存储库和实体包之前,我的代码仅使用一个存储库包就可以正常工作 class。代码失败,异常:... 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
方法采用可变参数:
EntityManagerFactoryBuilder.Builder packages(String... packagesToScan)
您调用它 3 次而不是在一次调用中传递 3 个包。
更一般地说:利用自动配置并遵循约定优于配置方法,如评论中所建议。
在我开始在我的数据源配置中添加更多存储库和实体包之前,我的代码仅使用一个存储库包就可以正常工作 class。代码失败,异常:... 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
方法采用可变参数:
EntityManagerFactoryBuilder.Builder packages(String... packagesToScan)
您调用它 3 次而不是在一次调用中传递 3 个包。
更一般地说:利用自动配置并遵循约定优于配置方法,如评论中所建议。