@Repository generated components are not recognised by graph if their indirect parents are requested
Assume I have the following class hierarchy:
interface FooRepository { ... }
@Repository
abstract class AbstractFooRepository( ... ) : FooRepository, JdbcRepository { ... }
@Generated
class `$AbstractFooRepository_Impl`( ... ) : AbstractFooRepository( ... ) {
...
}
If I then create the new component:
@Component
class FooService(val repo: FooRepository)
I get the following error:
Required dependency type FooRepository was not found and can't be autocreated.
But if I change repo type from FooRepository to AbstractFooRepository everything compiles just fine
Turned out that checking only requested type for @Repository annotation is the specific of Kora's Repository extension.
The intended way of solving the problem is adding:
@Module
interface FooRepositoryModule {
fun fooRepository(a: AbstractFooRepository): FooRepository = a
}
Can't guarantee 100% scan on all rounds
Advice: Use injection by class:
@Module
interface FooRepositoryModule {
fun fooRepository(a: AbstractFooRepository): FooRepository = a
}
In 0.13.1 version the above solution doesn't work anymore. But problem still persist.
I get the error:
Required dependency type FooRepository was not found and can't be autocreated.
but now when I try to fix it with module:
@Module
interface FooRepositoryModule {
fun fooRepository(a: AbstractFooRepository): FooRepository = a
}
I get the following error:
More than one component matches dependency claim FooRepository tag=[]:
TargetDependency(claim=DependencyClaim(type=FooRepository, tags=[], claimType=ONE_REQUIRED), component=ResolvedComponent(index=125, declaration=FromExtensionComponent(type=$AbstractFooRepository_Impl ...
TargetDependency(claim=DependencyClaim(type=FooRepository, tags=[], claimType=ONE_REQUIRED), component=ResolvedComponent(index=126, declaration=FromModuleComponent(type=FooRepository, module=MixedInModule(element=Application) ...
The only remaining fix for this issue that I found is manually instantiating $AbstractFooRepository_Impl:
@Module
interface FooRepositoryModule {
fun fooRepository(/* ... deps ... */): FooRepository = `$AbstractFooRepository_Impl`(/* ... deps ... */)
}
Try put @Component on your repository class.
Tried. Got the error:
ApplicationGraph.kt:1655:11: Cannot create an instance of an abstract class
component118 = graphDraw.addNode0(map["component118"],
arrayOf(),
{ AbstractFooRepository(
it.get(holder0.component117)
) },
listOf(),
component117
)
Kora version: 0.13.3
Now even my last fix (which btw worked in 0.11.10) to directly instantiate $AbstractFooRepository_Impl does not work.
The following Module:
@Module
interface FooRepositoryModule {
fun fooRepository(/* ... deps ... */): FooRepository = `$AbstractFooRepository_Impl`(/* ... deps ... */)
}
Makes task :kspKotlin run forever.