线程 "main" io.micronaut.context.exceptions.DependencyInjectionException 中的异常:无法为 class 的参数注入值
Exception in thread "main" io.micronaut.context.exceptions.DependencyInjectionException: Failed to inject value for parameter of class
我无法通过 Gradle 任务注入依赖项
我的build.gradle
task runDataFeeder(type:JavaExec){
classpath = sourceSets.main.runtimeClasspath
main = "example.migrator.RunMigrator"
}
runDataFeeder class 指向 RunMigrator class
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。如果类型是接口或抽象 class 此注释将尝试在编译时自动提供实现。
您还可以扩展 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 操作,例如您只允许实体 created/updated 而不是扩展 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
}
参见 Micronaut Guides - Access a database with JPA and Hibernate
的 2.4 存储库访问
我无法通过 Gradle 任务注入依赖项
我的build.gradle
task runDataFeeder(type:JavaExec){
classpath = sourceSets.main.runtimeClasspath
main = "example.migrator.RunMigrator"
}
runDataFeeder class 指向 RunMigrator class
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。如果类型是接口或抽象 class 此注释将尝试在编译时自动提供实现。
您还可以扩展 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 操作,例如您只允许实体 created/updated 而不是扩展 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
}
参见 Micronaut Guides - Access a database with JPA and Hibernate
的 2.4 存储库访问