実行時に選択したいくつかのエンティティにイベント リスナーを登録したいと考えています。
@LocalableEntity という名前のアノテーションを作成し、Google リフレクション 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);
}
}
それで、どうすればそれができますか?