Injecting EntityManager Vs. EntityManagerFactory

前端 未结 4 1492
遇见更好的自我
遇见更好的自我 2020-12-02 06:59

A long question, please bear with me.

We are using Spring+JPA for a web application. My team is debating over injecting EntityManagerFactory in the

4条回答
  •  春和景丽
    2020-12-02 07:40

    The pros and cons of injecting EntityManagerFactory vs EntityManager are all spelled out in the Spring docs here, I'm not sure if I can improve on that.

    Saying that, there are some points in your question that should be cleared up.

    ...Spring would create a new instance of a DAO for every web request...

    This is not correct. If your DAO is a Spring bean, then it's a singleton, unless you configure it otherwise via the scope attribute in the bean definition. Instantiating a DAO for every request would be crazy.

    The argument for injecting EMF is that its a good practice over all its always good to have a handle to a factory.

    This argument doesn't really hold water. General good practice says that an object should be injected with the minimum collaborators it needs to do its job.

提交回复
热议问题