java - Spring false positive circular dependency reported -


spring reports false positive circular dependency error when dependency order looks below

  • factorybean depends on list (example animalfeeder)
  • animalfeeder depends on list of strings.

interesting things are

  1. issue not observed when spring instantiation order changes ie) factorybean comes before animalfeeder. issue seen when animalfeeder comes before factorybean.
  2. this happens when factorybean involved. issue not observed when simple bean class used instead of factorybean.

here source code

public interface feeder {     void feed(); }  public class animalfeederimpl implements feeder {   private list<string> feedingtypes;   public animalfeederimpl(list<string> feedingtypes) {     this.feedingtypes = feedingtypes;   }   @override   public void feed() {     //feed here   } }  public class feedermanager {   private final list<feeder> feeders;   public feedermanager(list<feeder> feeders) {       this.feeders = feeders;   }    //this method trigger feeding every 4 hours   public void triggerfeeding() {    } }   public class feederfactory implements factorybean, initializingbean {   private list<feeder> feeders;   private feedermanager feedermanager;   public feederfactory(list<feeder> feeders) {       this.feeders = feeders;   }    @override   public void afterpropertiesset() throws exception {       feedermanager = new feedermanager(feeders);   }    public static void main(string args[]){     classpathxmlapplicationcontext context = new classpathxmlapplicationcontext();     context.setallowbeandefinitionoverriding(false);     context.setconfiglocation("test-application-context.xml");     context.refresh();   } } 

here context file

 <bean id="water" class="java.lang.string">     <constructor-arg value="water"/>  </bean>  <bean id="animalfeeder" class="org.test.spring.autowire.impl.animalfeederimpl" autowire="constructor"/>   <bean id="animalfeeder" class="org.test.spring.autowire.impl.animalfeederimpl" autowire="constructor"/>   <bean id="feedermgr" class="org.test.spring.autowire.impl.feederfactory" autowire="constructor"/> 

thanks!

the issue (which causes compilation error in seemingly incomplete example) feederfactory implements type factorybean.

public class feederfactory implements factorybean, initializingbean { 

when spring tries instantiate animalfeederimpl bean using constructor

public animalfeederimpl(list<string> feedingtypes) {     this.feedingtypes = feedingtypes; } 

it needs first construct list<string> argument. that, needs scan context beans of type string. knows sure water bean of type string because it's declared way in xml configuration. knows sure animalfeederimpl isn't bean of type string because class type declared.

for feederfactory, however, it's little different. declaring class subclass of factorybean, you're telling spring bean can create bean(s) of type. find out type is, spring needs instantiate feederfactory type , use getobjecttype method.

but instantiation requires spring autowire

public feederfactory(list<feeder> feeders) { 

which requires instantiate animalfeederimpl in construction , whole thing fails circular dependency.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -