0
votes

I am trying to introduce a new bean to existing project

Current bean is

package w.x.y.z.pkgA
@Component
public class BeanA implements InterfaceA {

}

And I am trying to add new Bean to w.x.y.z.pkgB

package w.x.y.z.pkgB
public class BeanB implements InterfaceB {

    @Autowired
    private BeanA beanA

    @PostConstruct
    public void postConstructMethod() {
        //Call some method in BeanA
    }
}

From BeanB I want to access BeanA data and in BeanB post construct I want to register BeanB with BeanA. So I want to call BeanA method

And All these packages are packaged as jar and spring context xml is

<context:annotation-config />
<context:component-scan base-package="w.x.y.z.pkgA,w.x.y.z.pkgB" />
<bean id="beanb" clas="w.x.y.z.pkgB.BeanB"></bean>

But during deployment I get exception about bean in create state

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'context': Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:347)
at w.x.y.z.BeanA<init>(BeanA.java:25)
at w.x.y.z.BeanB.<init>(BeanB.java:35)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)

I have also tried removed @Autowired and getting the BeanA object using getBeanFactory().getBean(BeanA.class). But I get same error.

If I remove bean entry from xml file them it gets deployed properly but post construct is never called as it is no longer a bean.

BeanA is not dependent on BeanB at all. No references?

Is there a way to get this @Autowired and @PostConstruct to work when 2 beans are in same jar?

1
Sorry, no idea what could create the possible circular reference. One final thought: Remove the explicit bean definition in your context file, add a @Component annotation to the bean since it is already in the scope for component scan and let spring do its best :) (deleted my misleading answer) - meistermeier
Before posting it here I have tried it. But no luck - Dheeraj Joshi

1 Answers

0
votes

You declare the bean as <bean id="beanb" clas="w.x.y.z.BeanB"> from w.x.y.z package.

Try with

package w.x.y.z
@Component
public class BeanA implements InterfaceA {

}


package w.x.y.z
public class BeanB implements InterfaceB {

    private BeanA beanA

    @PostConstruct
    public void postConstructMethod() {
        //Call some method in BeanA
    }

    // Getters and Setters
}

and

<context:annotation-config />
<context:component-scan base-package="w.x.y.z" />
<bean id="beanb" clas="w.x.y.z.BeanB">
    <property name="beanA" value="beanA"/> <!-- Spring will create beanA as it is annotated with @Component -->
</bean>