我无法通过Gradle任务注入(inject)依赖项
我的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: 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。如果类型是接口(interface)或抽象类,则此批注将尝试在编译时自动提供实现。
您还可以扩展CrudRepository接口(interface)以启用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
}
请参见Micronaut Guides - Access a database with JPA and Hibernate的2.4存储库访问