Java – automatically connect to sleep interceptor

I'm extending hibernate Emptyinterceptor, in my implementation, I want to automatically connect to some services, but they return null I added a @ Component annotation on 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 class:

@SuppressWarnings("serial")
@Component
public class AuditInterceptor extends EmptyInterceptor {

    @Autowired
    private IUserSessionService userSessionService;

Solution

I know it may be too late for two years - but I'm looking for the answer to the same question and think it's useful for some people in the future

Looking at the hibernate code, it looks like if you give the class name, hibernate will instantiate a new instance of the interceptor, but if you pass in a bean instance reference, it will use it

therefore

<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 bean myinterceptor is managed by spring, and automatic assembly will work!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>