Java 在运行时将hibernate事件侦听器注册到带注释的实体
我想将事件侦听器注册到我在运行时选择的一些实体 我创建了一个名为@LocalableEntity的注释,并通过谷歌反射api获取注释实体,所以我想向它们添加事件侦听器 我的代码在这里Java 在运行时将hibernate事件侦听器注册到带注释的实体,java,hibernate,hibernate-4.x,event-listener,Java,Hibernate,Hibernate 4.x,Event Listener,我想将事件侦听器注册到我在运行时选择的一些实体 我创建了一个名为@LocalableEntity的注释,并通过谷歌反射api获取注释实体,所以我想向它们添加事件侦听器 我的代码在这里 @Component public class HibernateEventWiring { @Autowired private SessionFactory sessionFactory; @Autowired private LocalizationListener listener; @PostCon
@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);
}
}
@组件
公共类HibernateEventWiring{
@自动连线
私人会话工厂会话工厂;
@自动连线
私有本地化侦听器;
@施工后
public void registerListeners(){
反射=新反射(“my.project.prefix”);
EventListenerRegistry=((SessionFactoryImpl)sessionFactory)。
getServiceRegistry()。
getService(EventListenerRegistry.class);
设置