自动装配以休眠拦截器 [英] Autowired to hibernate Interceptor
问题描述
我正在扩展hibernate.EmptyInterceptor,并且在我的实现中,我想自动装入某些服务,但它们返回null。我在类上添加了@Component注释。我的代码:
I'm extending hibernate.EmptyInterceptor and in my implementation I would like to have autowired to some services but they return null. I added a @Component annotation over the class. My code:
<property name="jpaPropertyMap">
<map>
<entry key="javax.persistence.transactionType" value="JTA" />
<entry key="hibernate.current_session_context_class" value="jta" />
<entry key="hibernate.transaction.manager_lookup_class"
value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup" />
<entry key="hibernate.connection.autocommit" value="false" />
<entry key="hibernate.ejb.interceptor" value="com.net.filter.AuditInterceptor"/>
</map>
</property>
和课程:
and the class :
@SuppressWarnings("serial")
@Component
public class AuditInterceptor extends EmptyInterceptor {
@Autowired
private IUserSessionService userSessionService;
推荐答案
我知道这可能是两年来太晚了 - 但我正在寻找同样问题的答案,并认为这对未来某个人会有用。
I know this is probably coming two years too late - but I was searching for an answer for the same problem, and thought this would be useful to someone in the future.
查看Hibernate代码看起来像Hibernate会实例化一个新的拦截器的实例,如果你给类的名字,但如果你传入一个bean实例引用它将使用它。
Looking at Hibernate code looks like Hibernate would instantiate a new instance of the interceptor if you give the class name, but if you pass in a bean instance reference it will use that.
所以
<bean id="myInterceptor" class="com.net.filter.AuditInterceptor" />
...
<property name="jpaPropertyMap">
<map>
<entry key="javax.persistence.transactionType" value="JTA" />
<entry key="hibernate.current_session_context_class" value="jta" />
<entry key="hibernate.transaction.manager_lookup_class"
value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup" />
<entry key="hibernate.connection.autocommit" value="false" />
<entry key="hibernate.ejb.interceptor" >
<ref bean="myInterceptor" />
</entry>
</map>
</property>
现在,bean myInterceptor被Spring管理,自动装配将起作用!
Now the bean myInterceptor is Spring managed and autowiring will work!
这篇关于自动装配以休眠拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!