-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Injecting EntityManagers through constructor injection (and at non-@PersistenceContext injection points in general) [SPR-10443] #15076
Comments
Oliver Drotbohm commented Created a ticket in Spring Data JPA to locally implement the support in there as it should be easy to add to the already registered bean definitions. |
Oliver Drotbohm commented We've implemented the support in Spring Data JPA and it seems generally useable even in a non Spring Data context. Activation of annotation based configuration could register the |
Marcel Overdijk commented To bad this is not possible yet. Especially in the light of http://olivergierke.de/2013/11/why-field-injection-is-evil/ |
Oliver Drotbohm commented Marcel Overdijk – Care to elaborate why the Spring Data JPA support for that is insufficient? |
Marcel Overdijk commented Oliver Drotbohm I have a project not using Spring Data. In a earlier post you mentioned support in Spring Data JPA which seems generally usable even in a non Spring Data context. What do you mean exactly with that? |
Francisco Lozano commented I also want to use constructor-injection of EntityManager without Spring Data JPA, feels weird that field injection is forced here. An |
Will this be fixed anytime soon? |
For the time being, a plain Spring application can define a bean of type Due to the separate nature of the shared @Bean
public EntityManager sharedEntityManager(EntityManagerFactory emf) {
return SharedEntityManagerCreator.createSharedEntityManager(emf);
} That's essentially what Spring Data JPA's post-processor ends up registering as well. If there is nothing else we can do at the core Spring Framework level, maybe we should explicitly show the approach above in the reference documentation. |
Oliver Drotbohm opened SPR-10443 and commented
Currently you can inject any Spring bean into clients through constructor injection. A JPA
EntityManager
is an exception to that. Constructor injection has the benefit of being able to design classes in an immutable way. Also, you communicate required dependencies through it easily. Sadly,@PersistenceContext
cannot be used on a constructor parameter and this probably won't change befor JPA 2.2 (giving 2.1 is close to final).It would be cool to simply be able to inject
EntityManager
instances through either@Autowired
or@Inject
.Issue Links:
12 votes, 17 watchers
The text was updated successfully, but these errors were encountered: