Exception in thread "main" io.micronaut.context.exceptions.DependencyInjectionException: Failed to inject value for parameter of class

huangapple go评论87阅读模式
英文:

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 类指向 RunMigrator 类

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: 无法为类的参数 [pharmacyRepository] 注入值:delivery.core.pharmacy.PharmacyService

消息:不存在类型为 [delivery.core.pharmacy.PharmacyRepository] 的 bean。请确保该 bean 未被 bean 要求禁用(启用“io.micronaut.context.condition”的跟踪日志以进行检查),如果启用了该 bean,则确保已将该类声明为 bean 并启用了注释处理(对于 Java 和 Kotlin,应将“micronaut-inject-java”依赖项配置为注释处理器)。
路径:new Migrator([PharmacySeedDataService pharmacySeedDataService]) --&gt; new PharmacySeedDataService([PharmacyService pharmacyService],RoleService roleService) --&gt; 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)
    ...
Caused by: io.micronaut.context.exceptions.NoSuchBeanException: 不存在类型为 [delivery.core.pharmacy.PharmacyRepository] 的 bean。请确保该 bean 未被 bean 要求禁用(启用“io.micronaut.context.condition”的跟踪日志以进行检查),如果启用了该 bean,则确保该类已声明为 bean 并启用了注释处理(对于 Java 和 Kotlin,应将“micronaut-inject-java”依赖项配置为注释处理器)。
    ...
英文:

I am not able to inject dependencies through Gradle task

My build.gradle

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

runDataFeeder class pointed to 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&lt;String&gt;) {
            ApplicationContext.run().use { applicationContext -&gt;
                val migrator: Migrator = applicationContext.getBean(Migrator::class.java)
                migrator.migrate()
            }
        }
    }
}

PharmacySeedDataService.kt contain

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

    fun createZone() {
       
    }
}

And PharmacyService.kt contain

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

Here is PharmacyRepository.kt

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

But after I ran gradle task, the following exception is thrown:

Exception in thread &quot;main&quot; 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 &#39;io.micronaut.context.condition&#39; 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 &#39;micronaut-inject-java&#39; dependency should be configured as an annotation processor).
Path Taken: new Migrator([PharmacySeedDataService pharmacySeedDataService]) --&gt; new PharmacySeedDataService([PharmacyService pharmacyService],RoleService roleService) --&gt; 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 &#39;io.micronaut.context.condition&#39; 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 &#39;micronaut-inject-java&#39; 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 &#39;io.micronaut.context.condition&#39; 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 &#39;micronaut-inject-java&#39; dependency should be configured as an annotation processor).

答案1

得分: 2

使用[@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 {
    // 在这里实现接口的声明方法
}

请参阅Micronaut Guides - 使用JPA和Hibernate访问数据库中的2.4 存储库访问部分。

英文:

Use an @Repository annotation to designate that PharmacyRepository is a type of a data repository that's then registered as a bean at runtime. If the type is an interface or abstract class this annotation will attempt to automatically provide implementations at compilation time.

You can also extend the CrudRepository interface to enable automatic generation of CRUD (Create, Read, Update, Delete) operations.

For instance:

import io.micronaut.data.annotation.*
import io.micronaut.data.model.*
import io.micronaut.data.repository.CrudRepository

@Repository
interface PharmacyRepository extends CrudRepository&lt;YOUR ENTITY HERE, YOUR ENTITY ID HERE&gt; {
}

If you don’t want to expose all CRUD operation, for example you only allow entities to be created/updated instead of extending CrudRepository you can extend GenericRepository.

Another option is to use a custom implementation of the PharmacyRepository to which you then add the @Repository annotation.

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
}

See 2.4 Repository Access of Micronaut Guides - Access a database with JPA and Hibernate

huangapple
  • 本文由 发表于 2020年7月26日 14:22:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63096793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定