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.
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!
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.