线程"main"中的异常 io.micronaut.context.exceptions.DependencyInjectionException:无法为类的参数注入值



我无法通过Gradle任务注入依赖项

我的建筑.gradle

task runDataFeeder(type:JavaExec){
classpath = sourceSets.main.runtimeClasspath
main = "example.migrator.RunMigrator"
}

指向RunMigrator类的runDataFeeder类

RunMigrator.kt

@Singleton
class Migrator(@Inject val pharmacySeedDataService: PharmacySeedDataService){
fun migrate(){
pharmacySeedDataService.createZone()
}
}
open class RunMigrator {
companion object {
@JvmStatic
fun main(args: Array<String>) {
ApplicationContext.run().use { applicationContext ->
val migrator: Migrator = applicationContext.getBean(Migrator::class.java)
migrator.migrate()
}
}
}
}

PharmacySeedDataService.kt包含

@Singleton
class PharmacySeedDataService(@Inject private val pharmacyService: PharmacyService, @Inject private val roleService: RoleService) {
fun createZone() {

}
}

PharmacyService.kt包含

@Singleton
class PharmacyService(@Inject val pharmacyRepository: PharmacyRepository){
}

这是PharmacyRepository.kt

@Singleton
interface PharmacyRepository {
fun createZone(zoneName: String, locationName: String): Zone
}

但在我运行gradle任务后,抛出了以下异常:

Exception in thread "main" io.micronaut.context.exceptions.DependencyInjectionException: Failed to inject value for parameter [pharmacyRepository] of class: delivery.core.pharmacy.PharmacyService
Message: No bean of type [delivery.core.pharmacy.PharmacyRepository] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
Path Taken: new Migrator([PharmacySeedDataService pharmacySeedDataService]) --> new PharmacySeedDataService([PharmacyService pharmacyService],RoleService roleService) --> new PharmacyService([PharmacyRepository pharmacyRepository])
at io.micronaut.context.AbstractBeanDefinition.getBeanForConstructorArgument(AbstractBeanDefinition.java:1016)
at delivery.core.pharmacy.$PharmacyServiceDefinition.build(Unknown Source)
at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1693)
at io.micronaut.context.DefaultBeanContext.createAndRegisterSingleton(DefaultBeanContext.java:2402)
at io.micronaut.context.DefaultBeanContext.getBeanForDefinition(DefaultBeanContext.java:2084)
at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:2058)
at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1095)
at io.micronaut.context.AbstractBeanDefinition.getBeanForConstructorArgument(AbstractBeanDefinition.java:1007)
at delivery.core.pharmacy.$PharmacySeedDataServiceDefinition.build(Unknown Source)
at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1693)
at io.micronaut.context.DefaultBeanContext.createAndRegisterSingleton(DefaultBeanContext.java:2402)
at io.micronaut.context.DefaultBeanContext.getBeanForDefinition(DefaultBeanContext.java:2084)
at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:2058)
at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1095)
at io.micronaut.context.AbstractBeanDefinition.getBeanForConstructorArgument(AbstractBeanDefinition.java:1007)
at delivery.core.$MigratorDefinition.build(Unknown Source)
at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1693)
at io.micronaut.context.DefaultBeanContext.createAndRegisterSingleton(DefaultBeanContext.java:2402)
at io.micronaut.context.DefaultBeanContext.getBeanForDefinition(DefaultBeanContext.java:2084)
at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:2058)
at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:623)
at delivery.core.RunMigrator$Companion.main(RunMigrator.kt:20)
at delivery.core.RunMigrator.main(RunMigrator.kt)
Caused by: io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [delivery.core.pharmacy.PharmacyRepository] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
Caused by: io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [delivery.core.pharmacy.PharmacyRepository] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).

使用@Repository注释指定PharmacyRepository是一种数据存储库类型,然后在运行时注册为bean。如果类型是接口或抽象类,则此注释将尝试在编译时自动提供实现。

您还可以扩展CrudRepository接口以启用CRUD(创建、读取、更新、删除(操作的自动生成。

例如:

import io.micronaut.data.annotation.*
import io.micronaut.data.model.*
import io.micronaut.data.repository.CrudRepository
@Repository
interface PharmacyRepository extends CrudRepository<YOUR ENTITY HERE, YOUR ENTITY ID HERE> {
}

如果您不想公开所有CRUD操作,例如,您只允许创建/更新实体,而不允许扩展CrudRepository,那么您可以扩展GenericRepository。

另一种选择是使用PharmacyRepository的自定义实现,然后向其中添加@Repository注释。

import io.micronaut.data.annotation.*
interface PharmacyRepository {
fun createZone(zoneName: String, locationName: String): Zone
}
...
@Repository
class PharmacyRepositoryImpl implements PharmacyRepository {
// implement declared methods of the interface here
}

请参阅2.4 Micronaut指南的存储库访问-使用JPA和Hibernate 访问数据库

相关内容

  • 没有找到相关文章

最新更新