栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring JPA / Hibernate EmptyInterceptor不注入Entitymanager / Spring Bean

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring JPA / Hibernate EmptyInterceptor不注入Entitymanager / Spring Bean

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>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/509162.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号