Announcement Announcement Module
No announcement yet.
Circular Dependency Nightmare Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Circular Dependency Nightmare

    We have an service based architecture and have recently been moving our transaction handling up from the DAO layer to the service layer. While doing this, I found a very nasty circular dependency that wasn't there before.

    I have a Catalog Service object that is the target of a TransactionFactoryProxyBean (CatalogTarget and Catalog respectively). The init-method of the CatalogTarget loads all the root categories from the database. These are represented by Category objects. These Category objects in turn only load the ID of their parent and child Categories. (Obviously at the root, it's only children). To actually return the Category object with a given ID, the Category objects use a reference to the Catalog to look it up. The problem here is that the Catalog Service object (which is actually the TransactionProxyFactoryBean) isn't initialized yet because it's waiting on the CatalogTarget to finish initializing. A reference to the Catalog Service can not be given to the Category objects and I get a nice stacktrace alerting me to my circular dependency. The reason it wasn't circular before was because the TransactionFactoryProxyBean targeted the DAO object which was then given to the Catalog Service object and by then the Catalog Service was ready to go during it's init-method.

    I'm trying to find a sane way out of this without changing the behavior of the Category objects. What I come up with is making a new bean whose only job is take a reference of Catalog (the one created from the TransactionFactoryProxyBean) and call the init-method from there instead of defining it on the bean definition of the Catalog Service target bean. Seems like a bit of a hack though. Is there a cleaner solution?


  • #2
    At the end of the day, you've got a circular dependency, so introducing a third object is probably the cleanest way to handle this...


    • #3
      Well, I guess it is the best I can do for now. I've already created the new 3rd object and it's working well.

      Thanks for the input.