是否可以在自定义BeanPostProcessor中访问HibernateEntityManagerFactory?

时间:2014-08-16 19:05:29

标签: spring hibernate spring-boot

在Spring Boot应用程序中,我试图抓住自定义HibernateEntityManagerFactory的{​​{1}},如:

BeanPostProcessor

但它给了我:

public class HibernateEventListenerBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware {

    private ApplicationContext applicationContext;
    private HibernateEntityManagerFactory entityManagerFactory;
    private EventListenerRegistry eventListenerRegistry;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        // TODO do the stuff with beans of various types
        return bean;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        this.entityManagerFactory = applicationContext.getBean(HibernateEntityManagerFactory.class);
    }
}

当我尝试在普通bean中自动装配Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.jpa.HibernateEntityManagerFactory] is defined 时,这可以正常工作。 E.g:

HibernteEntityManagerFactory

是否可以访问Spring @Component public class HibernateEventListenerRegistrar { private HibernateEntityManagerFactory entityManagerFactory; @Autowired public HibernateEventListenerRegistrar(HibernateEntityManagerFactory entityManagerFactory, List<HibernateEventListener> hibernateEventListeners) { this.entityManagerFactory = entityManagerFactory; } @PostConstruct public void registerListeners() { // TODO } } 中的HibernateEntityManagerFactory

1 个答案:

答案 0 :(得分:0)

以下代码将允许您注册Hibernate监听器:

@Configuration
public class HibernateExtraConfig {

    @Autowired
    private HibernateEntityManagerFactory entityManagerFactory;

    @Bean
    public EventListenerRegistry eventListenerRegistry() {
        SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) hibernateEntityManagerFactory.getSessionFactory();
        return sessionFactoryImpl.getServiceRegistry().getService(EventListenerRegistry.class);
    }
}

@Component
public class HibernateListenersConfigurer {

    private final EventListenerRegistry eventListenerRegistry;
    private final YourListener yourListener;

    @Autowired
    public HibernateListenersConfigurer(EventListenerRegistry eventListenerRegistry,
                                        YourListener yourListener) {

        this.eventListenerRegistry = eventListenerRegistry;
        this.yourListener= yourListener;

        configureListeners();
    }

    private void configureListeners() {
        eventListenerRegistry.getEventListenerGroup(EventType.POST_INSERT).appendListener(yourListenerctListener);
        eventListenerRegistry.getEventListenerGroup(EventType.POST_UPDATE).appendListener(yourListenerctListener);

        //whatever other events you need
    }
}

使用上面的代码,监听器看起来像:

public class YourListener implements PostInsertEventListener, PostUpdateEventListener {

   @Override
   public void onPostInsert(PostInsertEvent event) {

   }

   @Override
   public void onPostUpdate(PostUpdateEvent event) {

   }

   //you might also need this depending on your transaction configuration and what you are trying to do in  the listeners
   @Override
   public boolean requiresPostCommitHanding(EntityPersister persister) {
      return true;
   }

}