Search code examples
springmavenspring-web

How to resolve "cyclic loading of class path resource"?


I have two separate projects. One is maven, spring webservices(Let's say this is project A) and the other is maven batch project(Let's call it B). I am trying to inject a class(let's say class "batch.java") from B into a class(let's call it "api.java") of A. One major problem statement is that B has a dependency on A( on a different class of A, not on api.java).And I believe this is causing the cyclic loading error on startup.

I will paste my code here and I request to please provide a solution to it: file:B.xml

<import resource="classpath:A.xml" />
    <bean id="batch" class="XXX">
    <!-- lookup methods from project A -->
    </bean>

file:A.xml

 <import resource="classpath*:B.xml"/>
        <bean id="api" scope="prototype" lazy-init="true">
        <property name="batch" ref="batch" />
    </bean>

The code compiles fine but when I try to start my wsdl I get the following error: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:B.xml] Offending resource: class path resource [A.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:A.xml] Offending resource: class path resource [B.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Detected cyclic loading of class path resource [A.xml] - check your import definitions! org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)


Solution

  • Unfortunately you need to break the cyclic dependency.

    Put all commonly used bean definitions into a separate context and reference this context in your sub-contexts.

    Then you should end up with at least 3 context.xml's - eg:

    • common-context.xml (contains all bean definitions that are used in all sub-contexts)
    • a-context.xml (includes common-context.xml)
    • b-context.xml (includes common-context.xml)