Injecting EntityManager with a producer in tomcat

﹥>﹥吖頭↗ 提交于 2019-11-30 14:47:24

You need @Produces on your createEntityManager method, instead of the field.

The instance created through the Servlet specification by implementing ServletContextListener is not the same instance as CDI will create later on to resolve your @Produces field.

Further I recommend this informative blog post about the EntityManager: http://struberg.wordpress.com/2012/04/25/is-there-a-way-to-fix-the-jpa-entitymanager/

It might be slighly outdated though because if I recall correctly I have seen @TransactionScoped in the works over at deltaspike.

Lastly as you may have figured by now having a singleton EntityManager is not what you want but for the sake of completeness the miss in synergy between your ContextListener and CDI can be somewhat fixed by using CdiCtrl from Deltaspike and BeanProvider from Deltspike Core.

How to attach CDI ApplicationScope to to your current thread http://struberg.wordpress.com/2012/03/17/controlling-cdi-containers-in-se-and-ee/

And then you can pickup a bean that is annotated with @ApplicationScoped and set it up. The pickup is done with MyBean myBean = BeanProvider.getContextualReference(MyBean.class, false); http://deltaspike.apache.org/deltaspike/core.html

Hopefully CDI 1.1 will make this a little more fluent

Update: The talk on the Deltaspike list is to have 0.5 be a smaller release in about a month. Hopefully the new Servlet module will solve this nicely.

https://issues.apache.org/jira/browse/DELTASPIKE-376 https://issues.apache.org/jira/browse/DELTASPIKE-377

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!