Skip to content

Cannot inject repository into custom repository via constructor but with @Autowired #2319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Ch4s3r opened this issue Mar 2, 2021 · 3 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@Ch4s3r
Copy link

Ch4s3r commented Mar 2, 2021

The code below was working before updating to Spring version 2.4.3 on version 2.3.5.RELEASE.
It's not possible anymore to inject a repository via constructor into a custom repository, otherwise you get a circular dependency error.
Which you do not get if you use field injection with @Autowired.

Is this maybe a regression or did something get changed that this is not possible anymore in the new version?

@Entity
class User {
    @Id
    val id: Long = 0L
}

@Repository
interface UserRepository : JpaRepository<User, Long>, CustomUserRepository

interface CustomUserRepository {
    fun saveCustom(user: User): User?
}

class CustomUserRepositoryImpl(
    val userRepository: UserRepository
) : CustomUserRepository {

//    @Autowired
//    lateinit var userRepository: UserRepository

    override fun saveCustom(user: User)= userRepository.save(user)
}
┌─────┐
|  customUserRepositoryImpl defined in file [.../custom_repository/build/classes/kotlin/main/com/example/demo/CustomUserRepositoryImpl.class]
↑     ↓
|  userRepository defined in com.example.demo.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration
↑     ↓
|  (inner bean)#31691525
↑     ↓
|  customUserRepositoryImplFragment
└─────┘
@Ch4s3r
Copy link
Author

Ch4s3r commented Mar 2, 2021

Found a workaround by adding @Lazy to the constructor worked, but this seems more of like a workaround.

class CustomUserRepositoryImpl(
    @Lazy val userRepository: UserRepository
) : CustomUserRepository {

@Ch4s3r
Copy link
Author

Ch4s3r commented Mar 2, 2021

Just pinpointed the version.
With spring boot 2.4.0 it works and since 2.4.1 it's throwing a circular dependency error.

@schauder schauder transferred this issue from spring-projects/spring-data-jpa Mar 3, 2021
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 3, 2021
@mp911de
Copy link
Member

mp911de commented Mar 3, 2021

I think what you're experiencing here is a change in Spring Boot how JPA repositories are bootstrapped. Generally speaking, fragments are required at the time of constructing the actual repository. Therefore, you cannot inject a direct instance of the repository to be created into a fragment or a custom repository implementation. You can use @Lazy (proxying the bean) or a lookup via ApplicationContext or ObjectProvider.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants