3

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;
lior
  • 1,127
  • 3
  • 24
  • 43

2 Answers2

4

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.

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!

BhathiyaW
  • 356
  • 3
  • 11
3

Spring will never leave a @Autowired target as null (unless null is what you are injecting). That should tell you that if an @Autowired field is null, then Spring had nothing to do with it.

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.

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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I'm trying to inject a service to the class i posted. the service (IUserSessionService) is null. this service autowire works in different beans already. – lior Jan 14 '14 at 14:01
  • @lior I'd like to know which bean the properties you posted above belong to. That bean, instead of using your AuditInterceptor bean, is instantiating its own, which won't be managed by spring and therefore won't have any of its injection targets processed. – Sotirios Delimanolis Jan 14 '14 at 14:08
  • Thanks for your help but I found a solution in: http://stackoverflow.com/questions/19217872/injecting-jpas-entity-manager-in-hibernates-emptyinterceptor – lior Jan 15 '14 at 09:24