将Spring依赖注入到JPA EntityListener中 [英] Injecting a Spring dependency into a JPA EntityListener

查看:1429
本文介绍了将Spring依赖注入到JPA EntityListener中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Spring依赖项注入到 JPA EntityListener 中。这是我的监听器类:

I am trying to inject a Spring dependency into an JPA EntityListener. Here is my listener class:

@Configurable(autowire = Autowire.BY_TYPE, dependencyCheck = true)
public class PliListener {

    @Autowired
    private EvenementPliRepository evenementPliRepository;

    @PostPersist
    void onPostPersist(Pli pli) {
        EvenementPli ev = new EvenementPli();
        ev.setPli(pli);
        ev.setDateCreation(new Date());
        ev.setType(TypeEvenement.creation);
        ev.setMessage("Création d'un pli");
        System.out.println("evenementPliRepository: " + evenementPliRepository);
        evenementPliRepository.save(ev);
    }


}

这是我的实体类:

@RooJavaBean
@RooToString
@RooJpaActiveRecord
@EntityListeners(PliListener.class)
public class Pli implements Serializable{
...

但是,我的依赖(即 evenementPliRepository 始终为空

However, my dependency (i.e. evenementPliRepository) is always null.

任何人都可以帮忙?

推荐答案

我开始使用AOP将Spring bean注入Entity监听器。经过一天半的研究和尝试不同的事情,我遇到了这个链接其中指出:

I started to go down the path of using AOP to inject a spring bean into an Entity listener. After a day and a half of research and trying different things I came across this link which stated:


不可能将spring managed beans注入到JPA EntityListener类中。这是因为JPA侦听器机制应该基于无状态类,所以这些方法是有效的静态和非上下文感知的。 ...没有数量的AOP会保存你,没有任何东西注入到表示监听器的对象中,因为实现实际上并不创建实例,而是使用类方法。

It is not possible to inject spring managed beans into a JPA EntityListener class. This is because the JPA listener mechanism should be based on a stateless class, so the methods are effectively static, and non-context aware. ... No amount of AOP will save you, nothing gets injected to the ‘object’ representing the listener, because the implementations don’t actually create instances, but uses the class method.

在这一点上,我重新分组并偶然发现EclipseLink DescriptorEventAdapter 。使用这个信息我创建了一个扩展描述符适配器的监听器类。

At this point I regrouped and stumbled across the EclipseLink DescriptorEventAdapter. Using this information I created a listener class that extended the Descriptor Adapter.

public class EntityListener extends DescriptorEventAdapter {
    private String injectedValue;

    public void setInjectedValue(String value){
        this.injectedValue = value;
    }

    @Override
    public void aboutToInsert(DescriptorEvent event) {
       // Do what you need here
    }
}

为了使用类,我可以在我的实体类上使用@EntityListeners注释。不幸的是,这种方法不允许Spring控制我的监听器的创建,结果不允许依赖注入。相反,我将以下'init'函数添加到我的类中:

In order to use the class I could have used the @EntityListeners annotation on my entity class. Unfortunately, this method would not allow Spring to control the creation of my listener and as a result would not allow for dependency injection. Instead I added the following 'init' function to my class:

public void init() {
    JpaEntityManager entityManager = null;

    try {
        // Create an entity manager for use in this function
        entityManager = (JpaEntityManager) entityManagerFactory.createEntityManager();
        // Use the entity manager to get a ClassDescriptor for the Entity class
        ClassDescriptor desc = 
            entityManager.getSession().getClassDescriptor(<EntityClass>.class);
        // Add this class as a listener to the class descriptor
        desc.getEventManager().addListener(this);
    } finally {
        if (entityManager != null) {
            // Cleanup the entity manager
            entityManager.close();
        }
    }
}

添加一点Spring XML配置

Add a little Spring XML configuration

<!-- Define listener object -->
<bean id="entityListener" class="EntityListener " init-method="init">
    <property name="injectedValue" value="Hello World"/>
    <property name="entityManagerFactory" ref="emf"/>
</bean>  

现在我们有一种情况,Spring创建一个实体监听器,用任何依赖关系注入它,监听器对象注册自己想要收听的实体类。

Now we have a situation where Spring creates a entity listener, injects it with whatever dependencies are needed, and the listener object registers itself with the entity class to which it intends to listen.

我希望这有助于。

这篇关于将Spring依赖注入到JPA EntityListener中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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