11

I have this exception:

SEVERE: Context initialization failedorg.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'myService': Bean with name 'myService' has been injected into other beans [otherService] 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.

Can anyone suggest good strategies for finding where the circular dependency comes from ?

I am currently looking through the context definition, but as you might imagine in a project of some maturity this takes quite a while.

So I'm generally looking for ideas on quickly finding circular bean dependencies.

Simeon
  • 7,582
  • 15
  • 64
  • 101
  • Are you using XML, annotations, or configuration classes for your config? – atrain Dec 06 '11 at 15:14
  • 2
    Here's a good article on resolving: http://blog.richardadamdean.com/?p=49 – atrain Dec 06 '11 at 15:19
  • Isn't the error message trying to say that the bean `otherService` is at fault? Is there some way that `otherService` depends on `myService` but `myService` also depends on `otherService`? Are you asking how to see the bean linkages so you can detect how the dependencies go circular? – Gray Dec 06 '11 at 16:17
  • @Gray No they don't depend on one another. Not directly anyway, this is what I'm trying to find. It's probably something like otherService->3rdService->myservice AND myService->3rdService->otherService. – Simeon Dec 07 '11 at 08:14
  • I will find it eventually by digging through the context, but I was wandering if someone knew a fast/sure way to do it. – Simeon Dec 07 '11 at 08:15
  • Related to: http://stackoverflow.com/questions/5553883/finding-spring-bean-references – Gray Dec 07 '11 at 13:42

4 Answers4

8

Here's 2 tools that advertise dependency graph generation. I don't have any experience with them however.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • 1
    If you use one of them, add a comment on how it worked if you can. Best of luck. Thanks for the check. – Gray Dec 08 '11 at 15:44
  • 1
    the bean dependency graph for eclipse is nice, it draws pretty graphics that really help. You just need to add a spring project nature to one of your projects, switch to the spring eclipse view, right click your project and click draw dependency graph on one of your beans. – Simeon Dec 15 '11 at 12:19
4

One approach that worked for me was to write a helper class with a main method that instantiates a refreshable application context, disallows circular dependencies and calls refresh. Make sure that your beans / app context config files are on the classpath.

public class ContextChecker {
    public static void main( String[] args ) {
        AbstractRefreshableApplicationContext ctx = 
            new ClassPathXmlApplicationContext( "classpath*:/beans.xml" );
        ctx.setAllowCircularReferences( false );
        ctx.refresh();
    }
}

This class can be run as a Java application, e.g. from inside your IDE. If circular dependencies are detected in your context, the error messages will provide information about the offending beans.

lars
  • 640
  • 4
  • 10
  • This will not work reliably, because the order in which the dependencies are assembled is unspecified and could change from one runtime environment to the next. In other words, you could have a working setup locally that will fail on the production server. – ulim Apr 27 '23 at 10:53
4

I know that OP's case is different, but for future reference:

If you have this exception:

  • org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'a': Requested bean is currently in creation: Is there an unresolvable circular reference?

Then the message at the top of the stack trace should say what is the dependency cycle, e.g.:

  • org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'a' defined in file [filename]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.gk.simple.B]: Error creating bean with name 'b' defined in file [filename]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.gk.simple.A]: Error creating bean with name 'a': Requested bean is currently in creation: Is there an unresolvable circular reference?

Here the cycle is a -> b -> a.

X. Wo Satuk
  • 923
  • 1
  • 11
  • 18
3

IntelliJ gave me a pretty good dependency graph althou I had to increase the Xmx size to provide enough memory for the operation. Right click on the application context file, Diagrams> Dependency diagram. Sometimes it takes a while to load. If in case the annotation based app context doesnt work, try to move the component scan to application.xml and try again.

Gautam Pal
  • 51
  • 2