Skip to content
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

Injection of repository into config classes with infrastructure components fails [DATAJPA-408] #816

Closed
spring-projects-issues opened this issue Oct 3, 2013 · 2 comments

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Oct 3, 2013

Petri Kainulainen opened DATAJPA-408 and commented

If Spring Data JPA is configured by using Java configuration, the repositories are not scanned from external jar files. If you use XML configuration, everything works as expected.

An example project is available here:
https://github.com/dilgerma/spring-data-tutorial


No further details from DATAJPA-408

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 3, 2013

Oliver Drotbohm commented

Let's make this a bit more precise. The issue as nothing to do with where the repository interfaces are located. The error is triggered by the injection point in the ApplicationConfig referring to a BookStoreRepository. Here's the story for why this fails.

The injection of the repository into the infrastructure configuration effectively creates a cyclic dependency as follows: @EnableJpaRepositories triggers the detection of the repository interface. To create the proxy instance for the repository we need an EntityManagerFactory. Spring now scans the configuration class, detects, that it's a factory class for an EMF and needs to create an instance of it to call the factory method. After it has created an instance of it it processes the class' injection points, and now needs an instance of the repository, which it was just about to create and throws the exception.

Within the 1.3.x versions this fact is a bit hidden in the initialization of the PersistenceExceptionTranlslationInterceptor (if you skimm through the stack trace closely you see it triggering the scan for PersistenceExceptionTranslator instances - an interface Spring's EMFB implement, eventually triggering the circular dependency). With Spring Data JPA 1.4.1 you get a more speaking exception message indicating the issue in more detail.

Long story short, here are a few workarounds:

  1. Do you really need a reference to the repo from within the infrastructure config? JavaConfig is a bit of a different beast as the configuration classes effectively become Spring beans and thus the lifecycle is slightly more complicated. If you can avoid the reference, the better.
  2. As of Spring 4 M3, you can use @Lazy at the injection point so that the container will create a proxy backed by a LazyInitTargetSource for this injection point which allows getting a reference to the repository before it's even eventually materialized (see the requesting ticket SPR-10353 for details). This will get the tests to run, but might blow up once you use the repo as the lazy-init proxy will need to create the bean on first access.
  3. It might also work - I haven't tested, admittedly - to rather use method injection (pass a dependency to the {{@Bean} method) in the config class as the contain will then inject it when calling the factory method, not on config class instantiation already

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented May 22, 2019

Jens Schauder commented

Batch closing resolved issue without a fix version and a resolution indicating that there is nothing to release (Won't fix, Invalid ...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants