Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wire a SessionFactory into a Hibernate Interceptor with Spring? [duplicate]

I want to declare two beans and instantiate them using Spring dependency injection?

<bean id="sessionFactory" class="SessionFactoryImpl">
 <property name="entityInterceptor" ref="entityInterceptor"/>
</bean>

<bean id="entityInterceptor" class="EntityInterceptorImpl">
 <property name="sessionFactory" ref="sessionFactory"/>
</bean>

But Spring throws an exception saying "FactoryBean which is currently in creation returned null from getObject"

Why is inter-dependent bean wiring not working here? Should i specify defferred property binding anywhere?

like image 842
Sathish Avatar asked Nov 24 '22 13:11

Sathish


2 Answers

Unfortunately the way container initialization works in Spring, a bean can only be injected in another bean once it is fully initialized. In your case you have a circular dependency that prevents either bean to be initialized because they depend on each other. To get around this you can implement BeanFactoryAware in one of the beans and obtain the reference to the other bean using beanFactory.getBean("beanName").

like image 182
neesh Avatar answered Dec 05 '22 12:12

neesh


neesh is right, Spring doesn't do this out of the box.

Interdependent beans hint at a design problem. The "clean" way to do this is to redesign your services in such a way that there are no such odd dependencies, of course provided that you have control over the implementations.

like image 43
Henning Avatar answered Dec 05 '22 12:12

Henning