该
AuditEmptyInterceptor不是由Spring管理的豆,它是由Hibernate来实例化,所以你不能依赖注入它。
您可以改用静态委托:
public class StaticDelegateInterceptor extends EmptyInterceptor { private static Interceptor interceptor; public static void setInterceptor(Interceptor interceptor) { StaticDelegate.interceptor = interceptor; } public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { return StaticDelegate.interceptor.onSave(entity, id, state, propertyNames, types); } ...}
在页面上注册StaticDelegateInterceptor
persistence.xml
<persistence> <persistence-unit name="xxx" transaction-type="RESOURCE_LOCAL"> <class>com.xxx</class> ... ... <properties> <property name="hibernate.ejb.interceptor" value="com.company.demo.audit.StaticDelegateInterceptor" /> </properties> </persistence-unit></persistence>
修改您当前的AuditEmptyInterceptor,使其在StaticDelegateInterceptor中进行注册:
@Named@Transactionalpublic class AuditEmptyInterceptor extends EmptyInterceptor { @PostConstruct public void init() { StaticDelagateInterceptor.setInterceptor(this); } ...}
最后
entityManagerFactory,
auditEmptyInterceptor通过设置depends-on属性来确保您的bean依赖于您:
<bean id="entityManagerFactory"p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="jpaAdapter"depends-on="auditEmptyInterceptor" >...</bean>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)