如果您使用SessionFactory,则将是以下配置:
<bean id="mySessionFactory" > <!-- Stripped other stuff --> <property name="eventListeners"> <map> <entry key="pre-load"> <bean /> </entry> <entry key="pre-persist"> <bean /> </entry> </map> </property></bean>
但是由于您使用的是JPA,因此恐怕您需要使用此线程中概述的AOP
或者你可以
- 将ApplicationContext存储在ThreadLocal或自定义的holder类中,并通过静态方法公开它
- 为您的听众提供一个基类,如下所示:
基类:
public abstract class Listenerbase{ protected void wireMe(){ ApplicationContext ctx = ContextHelper.getCurrentApplicationContext(); ctx.getAutowireCapableBeanFactory().autowireBean(this); }}现在在您的lifycycle方法中
wireMe()先调用。
更新:
这是一个示例实现
ContextHelper:
public final class ContextHelper implements ApplicationContextAware{ private static final ContextHelper INSTANCE = new ContextHelper(); private ApplicationContext applicationContext; @Override public void setApplicationContext(final ApplicationContext applicationContext){ this.applicationContext = applicationContext; } public static ApplicationContext getCurrentApplicationContext(){ return INSTANCE.applicationContext; }; public static ContextHelper getInstance(){ return INSTANCE; } private ContextHelper(){ }}将其连接到您的Spring Bean配置中,如下所示:
<bean factory-method="getInstance" />



