Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to activate circular dependencies in Play 2.5?

This question originates from a question that I asked two days ago. I implemented my own error handler which extends DefaultHttpErrorHandler. DefaultHttpErrorHandler extends HttpErrorHandler which is also used as a parameter errorHandler: HttpErrorHandler in the WebJarAssets class. First I thought I had a flaw in my design, but James Ward commented that it seems like that I am doing things right.

Long story short, I need to know how I can activate circular dependencies. Unfortunately, there is no example code listed in the Play documentation, so I have no idea where I should set disableCircularProxies(false).

like image 747
John Doe Avatar asked May 08 '16 14:05

John Doe


People also ask

How do you fix a circular dependency problem?

There are a couple of options to get rid of circular dependencies. For a longer chain, A -> B -> C -> D -> A , if one of the references is removed (for instance, the D -> A reference), the cyclic reference pattern is broken, as well. For simpler patterns, such as A -> B -> A , refactoring may be necessary.

How do you manage circular dependency?

To reduce or eliminate circular dependencies, architects must implement loose component coupling and isolate failures. One approach is to use abstraction to break the dependency chain. To do this, you introduce an abstracted service interface that delivers underlying functionality without direct component coupling.

What causes circular dependency?

A circular dependency occurs when two classes depend on each other. For example, class A needs class B, and class B also needs class A. Circular dependencies can arise in Nest between modules and between providers. While circular dependencies should be avoided where possible, you can't always do so.

What is circular dependency problem?

In software engineering, a circular dependency is a relation between two or more modules which either directly or indirectly depend on each other to function properly. Such modules are also known as mutually recursive.


1 Answers

You need a custom GuiceApplicationLoader like:

import play.api.ApplicationLoader
import play.api.inject.guice.{GuiceApplicationLoader, GuiceApplicationBuilder}

class CustomApplicationLoader extends GuiceApplicationLoader {
  override protected def builder(context: ApplicationLoader.Context): GuiceApplicationBuilder = {
    super.builder(context).disableCircularProxies(false)
  }
}

And tell Play to use it in application.conf:

play.application.loader = "CustomApplicationLoader"

Full code example: https://github.com/webjars/webjars-play/tree/cicular-deps

like image 172
James Ward Avatar answered Oct 27 '22 00:10

James Ward