2

我想将事件侦听器注册到我在运行时选择的一些实体。

我创建了一个名为 @LocalableEntity 的注释,并通过谷歌反射 api 获取注释实体,所以我想向它们添加事件侦听器。

我的代码在这里;

@Component
public class HibernateEventWiring {

@Autowired
private SessionFactory sessionFactory;

@Autowired
private LocalizationListener listener;

@PostConstruct
public void registerListeners() {

    Reflections reflections = new Reflections("my.project.prefix");
    EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory).
                                       getServiceRegistry().
                                       getService(EventListenerRegistry.class);


     Set<Class<?>> annotated =  reflections.
                                getTypesAnnotatedWith(LocalableEntity.class);

     for(Class<?> clazz : annotated) {

         // I want to add listener to these annotated classses

     }

    // this code line registering listener for all entities
    registry.getEventListenerGroup(EventType.PRE_LOAD).appendListener(listener);


    }  
}

那么,我该怎么做呢?

4

0 回答 0