I have a spring boot application which runs well on my local, UAT, and Prod environments but when I deploy it in a staging environment, it gives the following error:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Bean 1': Unsatisfied dependency expressed through field 'Class 1'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'Bean2 of type Class1': Bean with the name 'Bean2' has been injected into other beans [Bean 3] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.

I am not sure if it's related to the build process or what. I have checked the code, there are no circular dependencies. When I annotate Bean2 with @Lazy on Class3, it works well. But I am not able to figure out why it works on other environments and this annotation is needed only for the Staging.

Comment From: wilkinsona

I have checked the code, there are no circular dependencies.

This can't be the case. You won't get a BeanCurrentlyInCreationException unless there is a circular dependency between beans.

Framework contains some logic that attempts to break a dependency cycle, but its success depends on lots of factors and not all of those are constant. For example, it will vary based on the precise order in which beans are defined which depends on the OS, JVM, filesystem, etc.

When I annotate Bean2 with @Lazy on Class3, it works well

This is further evidence that there is a cycle in your beans' dependencies. Adding @Lazy provides another option to Framework for breaking the cycle.

If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

Comment From: harshitdx29

Unfortunately, that is a an organization code and I won't be able to share it. I will try to investigate more on circular dependency. The maximum I found was Class 1 depends On 2, and Class 3 depends on both Class 1 and Class 2 but clearly this is not a dependency.

Also, what is the OS, JVM, factors which help decide beans order?

Comment From: wilkinsona

Also, what is the OS, JVM, factors which help decide beans order?

It's things like the order in which class files are read from the filesystem, methods are found in a class by reflection, etc. IIRC, Framework protects against some of these to make things deterministic but you will still see some variation.

Without a sample, I don't think we're going to be able to help you here. It's also not clear that there's a bug, particularly one in Spring Boot as resolving bean dependencies is done by Spring Framework. With it being unlikely that a change in Spring Boot is required, I'm going to close this issue. If you'd like some further assistance diagnosing the problem, please follow up on Stack Overflow or Gitter.