本文介绍了CDI注入EntityListeners中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 由于JPA 2.0不支持注入EntityListener(JPA 2.1 will),因此决定使用JNDI查找来获取 BeanManager 并通过它获取登录用户。我定义了一个类似于这个的 EntityListener : public class MyEntityListener { public static BeanManager getBeanManager(){ try { InitialContext initialContext = new InitialContext(); return(BeanManager)initialContext.lookup(java:comp / BeanManager); } catch(NamingException e){ e.printStackTrace(); 返回null; public Object getBeanByName(String name){ BeanManager bm = getBeanManager(); Bean bean = bm.getBeans(name).iterator()。next(); CreationalContext ctx = bm.createCreationalContext(bean); 返回bm.getReference(bean,bean.getClass(),ctx); } @PrePersist @PreUpdate public void onPreInsertOrUpdate(MyEntity entity){ User loggedInUser =(User)getBeanByName(loggedInUser); entity.setUpdatedUser(loggedInUser); entity.setUpdatedTimestamp(new Date()); 用户在会话范围内被管理为: @SessionScoped public class UserManager实现Serializable { private User loggedInUser; @Named(loggedInUser) public User getLoggedInUser(){ return loggedInUser; } //在成功登录后设置登录用户} 我想知道是否有任何缺点或要点注意这种方法。性能吞吐量?当多个登录用户在自己的作用域中同时更新实体时会发生什么? Hibernate JPA 2.0 Seam Weld CDI Glassfish 3.1.2 解决方案您的方法是正确的。性能吞吐量? 恕我直言,无需担心 - JPA 2.1将使用等效机制。但请确保写一个逼真的测试以保证安全。 当多个登录用户在自己的作用域中同时更新实体时会发生什么? 所有(非依赖范围的)bean引用都在内部进行代理。底层的CDI实施必须保证正确的分辨率。 Since JPA 2.0 does not support injection into EntityListener (JPA 2.1 will), decided to use JNDI lookup to get the BeanManager and through it get the logged in user. I defined an EntityListener similar to this:public class MyEntityListener { public static BeanManager getBeanManager() { try { InitialContext initialContext = new InitialContext(); return (BeanManager) initialContext.lookup("java:comp/BeanManager"); } catch (NamingException e) { e.printStackTrace(); return null; } } public Object getBeanByName(String name) { BeanManager bm = getBeanManager(); Bean bean = bm.getBeans(name).iterator().next(); CreationalContext ctx = bm.createCreationalContext(bean); return bm.getReference(bean, bean.getClass(), ctx); } @PrePersist @PreUpdate public void onPreInsertOrUpdate(MyEntity entity) { User loggedInUser = (User) getBeanByName("loggedInUser"); entity.setUpdatedUser(loggedInUser); entity.setUpdatedTimestamp(new Date()); }}User is managed in session scope as:@SessionScopedpublic class UserManager implements Serializable { private User loggedInUser; @Produces @Named("loggedInUser") public User getLoggedInUser() { return loggedInUser; } // Set the logged in user after successfully login action}I want to know is there any disadvantages or points to pay attention of this approach. Performance throughput? What happens when there are multiple logged in users updating entities concurrently in their own scopes?Hibernate JPA 2.0Seam Weld CDIGlassfish 3.1.2 解决方案 Your approach is correct. Performance throughput?IMHO no need to worry - JPA 2.1 will use an equivalent mechanism. But make sure to write a realistic test to be on the safe side. What happens when there are multiple logged in users updating entities concurrently in their own scopes?All (non-dependend-scoped) bean references are proxied internally. The underlaying CDI-implementation has to guarantee the correct resolution. 这篇关于CDI注入EntityListeners中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-25 18:47