简体   繁体   English

自动连接到休眠拦截器

[英]Autowired to hibernate Interceptor

I'm extending hibernate.EmptyInterceptor and in my implementation I would like to have autowired to some services but they return null. 我正在扩展hibernate.EmptyInterceptor,在我的实现中,我希望自动连接到某些服务,但它们返回null。 I added a @Component annotation over the class. 我在类上添加了一个@Component注释。 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. 我知道这可能要来两年太晚了 - 但我正在寻找同样问题的答案,并认为这对未来的某些人有用。

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. 查看Hibernate代码看起来如果给出类名,Hibernate将实例化拦截器的新实例,但如果传入bean实例引用,它将使用它。

So 所以

<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>

Now the bean myInterceptor is Spring managed and autowiring will work! 现在bean myInterceptor是Spring管理的,自动装配将工作!

Spring will never leave a @Autowired target as null (unless null is what you are injecting). Spring 永远不会@Autowired目标保留为null (除非null是你注入的)。 That should tell you that if an @Autowired field is null , then Spring had nothing to do with it. 这应该告诉你,如果@Autowired字段为null ,那么Spring与它无关。

It seems that is the case here. 这似乎就是这种情况。 By providing something like 通过提供类似的东西

<entry key="hibernate.ejb.interceptor" value="com.net.filter.AuditInterceptor"/>

I believe you're telling Hibernate to create that instance itself and it therefore won't be a Spring managed bean. 我相信你告诉Hibernate创建该实例本身,因此它不会是一个Spring托管bean。

If you post the rest of the bean definition because I don't know what bean you are trying to inject into, there might be alternatives. 如果你发布剩下的bean定义,因为我不知道你想要注入什么bean,可能有其他选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM