在JPA实体侦听器中注入spring bean [英] Injecting spring bean in a JPA Entity Listener

查看:241
本文介绍了在JPA实体侦听器中注入spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过将JPA实体侦听器标记为@Configurable使其了解spring上下文.但是注入的四季豆为空.能够使JPA实体使用相同的技术了解Spring上下文.我正在使用Spring(core和data-jpa)作为基础架构.关于如何使用JPA实体监听器或spring data-jpa实现此目标的任何想法?

Am trying to make a JPA Entity Listener aware of the spring context by marking it as @Configurable. But the injected spring beans are null. Am able to make JPA entities aware of the Spring context using the same technique. Am using Spring(core and data-jpa) as infrastructure. Any ideas on how to acheive this using JPA Entity Listeners or spring data-jpa?

@Configurable
@Scope("singleton")
public class AggregateRootListener {
    private static Logger log = LoggerFactory.getLogger(AggregateRootListener.class);

    @Autowired
    private EventHandlerHelper eventHandlerHelper;

    @PostPersist
    @PostUpdate
    public void publishEvents(BaseAggregateRoot aggregateRoot){
        log.info(aggregateRoot.getEvents().toString());
        aggregateRoot.getEvents().stream()
            .forEach(event -> {
                eventHandlerHelper.notify(event, aggregateRoot);
                log.info("Publishing " + event + " " + aggregateRoot.toString());
            });
    }
}

和BaseAggregateRoot代码

and the BaseAggregateRoot code

@Configurable
@Scope("prototype")
@MappedSuperclass
@EntityListeners(AggregateRootListener.class)
public abstract class  BaseAggregateRoot extends BaseDomain{
    public static enum AggregateStatus {
        ACTIVE, ARCHIVE
    }

    @EmbeddedId
    @AttributeOverrides({
          @AttributeOverride(name = "aggregateId", column = @Column(name = "ID", nullable = false))})
    protected AggregateId aggregateId;



    @Version
    private Long version;
}

推荐答案

事件侦听器机制是JPA概念,由JPA提供程序实现.我不认为Spring会创建事件侦听器类实例-它们是由JPA提供程序(Hibernate,EclipseLink等)创建的.因此,常规的Spring注入不适用于事件侦听器类实例. 此帖子的作者似乎来了相同的结论.

Event Listener mechanism is a JPA concept and is implemented by the JPA provider. I don't think Spring creates event listener class instances - they are rather created by the JPA provider (Hibernate, EclipseLink, etc.). Therefore, the regular Spring injection would not work with event listener class instances. The author of this post seems to have come to the same conclusion.

也就是说,我在JPA事件监听器中确实使用了Spring托管的bean.我使用的解决方案是为在非Spring管理的所有类中获取Spring bean实例而开发的.它涉及创建以下类:

That said, I do use Spring managed beans in JPA event listeners. The solution I use was developed to get hold of Spring bean instances in all classes that are not managed by Spring. It involves creating the following class:

@Component
public class SpringApplicationContext implements ApplicationContextAware {
  private static ApplicationContext CONTEXT;

  public void setApplicationContext(final ApplicationContext context)
              throws BeansException {
    CONTEXT = context;
  }

  public static <T> T getBean(Class<T> clazz) { return CONTEXT.getBean(clazz); }
}

此类在初始加载时缓存Spring应用程序上下文.然后,该上下文用于查找Spring托管的bean.

This class caches the Spring application context at initial load. The context is then used to look up Spring managed beans.

使用类就像SpringApplicationContext.getBean(FooService.class)一样简单.

所有常规的Spring语义(例如bean生命周期,bean作用域和可传递依赖项)都得到处理.

All the usual Spring semantics, such as, bean lifecycle, bean scope and transitive dependencies are taken care of.

这篇关于在JPA实体侦听器中注入spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆