-
Notifications
You must be signed in to change notification settings - Fork 38.4k
Cyclic dependency with setter injection and Java Config #25443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for the four Java source files. Unfortunately, they don't provide enough information to diagnose the problem. Can you please provide a complete minimal project including the XML configuration, pom.xml or build.gradle, etc that reproduces the failure that you're seeing? |
Hi Andy Wilkinson, PFA. Spring Boot Core Maven Project.
In Both cases, I'm trying to do Circular dependencies with Setter Dependency Injection. Please let me know if you need any more info from my end. Thank you in advance. |
Thanks for the complete sample. The problem is in your @Configuration
public class JavaConfig {
@Bean
public PaymentProcesser paymentProcessor(){
return new PaymentProcesser();
}
@Bean
public OrderConfirmation orderConfirmation() {
return new OrderConfirmation();
}
} In addition, you need to add @Autowired
public void setOc(OrderConfirmation oc) {
this.oc = oc;
} @Autowired
public void setPp(PaymentProcesser pp) {
this.pp = pp;
} If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. |
Re-opening so the issue can be transferred to Spring Framework's issue tracker. |
An alternative solution exists that does not require the use of @Configuration
public class JavaConfig {
@Bean
public PaymentProcesser pp(OrderConfirmation oc) {
PaymentProcesser pp = new PaymentProcesser();
pp.setOc(oc);
return pp;
}
@Bean
public OrderConfirmation oc() {
OrderConfirmation oc = new OrderConfirmation();
oc.setPp(pp(oc));
return oc;
}
} The main method also needs to be updated, however, since there is no bean named OrderConfirmation sp = context.getBean(OrderConfirmation.class); Note, however, that the above alternative solution would not be sufficient if the If you attempt to reference each of the beans by type via @Configuration
public class JavaConfig {
@Bean
public PaymentProcesser pp(OrderConfirmation oc) {
PaymentProcesser pp = new PaymentProcesser();
pp.setOc(oc);
return pp;
}
@Bean
public OrderConfirmation oc(PaymentProcesser pp) {
OrderConfirmation oc = new OrderConfirmation();
oc.setPp(pp);
return oc;
}
} ... Spring Boot will then inform you of the circular dependency as follows.
Although there are viable workarounds for the example you've provided, the question still remains why there is a difference between the XML and Java configuration. The answer likely lies in the fact that the XML configuration is ultimately just metadata that Spring uses to create In any case, we'll see if there's anything that can be done, and otherwise we'll consider adding a note to the documentation for such scenarios. |
I previously said that would not work; however, it does work if you annotate one of the This approach can be used to circumvent circular dependency issues between @Configuration
public class JavaConfig {
@Bean
public PaymentProcesser pp(OrderConfirmation oc) {
PaymentProcesser pp = new PaymentProcesser();
pp.setOc(oc);
return pp;
}
@Bean
public OrderConfirmation oc(@Lazy PaymentProcesser pp) {
OrderConfirmation oc = new OrderConfirmation();
oc.setPp(pp);
return oc;
}
} @ELearnTez, although that does not definitively answer your question, does that meet your needs in terms of a viable solution? |
In light of the above solution using |
Thank you Much for the Consideration |
Is there a kind of post-configurer for Beans like the following fictional example to separate bean instantiation from initialization?
|
Hi,
I'm trying to run simple spring core with Spring Circular dependency + Setter Dependency Injection + Java Config => Getting Exception. It's working fine with XML config. but its failing with java config. I'm not able to understand what is the problem.
Attached required files to replicate the issue locally.
Spring Circular dependency + Setter Dependency Injection + Java Config => Getting Exception
Spring Circular dependency + Setter Dependency Injection + XMLConfig => Working Fine
Attached Files for debugging purpose
Exception in thread "main" java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanExceptionException in thread "main" java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
circular.zip
Related Issues
The text was updated successfully, but these errors were encountered: