title: Avoiding framework-imposed circular dependencies in Guice
body: <p><strong>Please note:</strong> Although this question specifically calls out Swing, I <em>believe</em> this to be a pure Guice (<strong>4.0</strong>) question at heart and which highlights a potential generic issue with Guice and other opinionated frameworks.</p>
  
  <hr>
  
  <p>In Swing, you have your application UI, which extends <code>JFrame</code>:</p>
  
  <pre><code>// Pseudo-code
  class MyApp extends JFrame {
      // ...
  }
  </code></pre>
  
  <p>Your app (<code>JFrame</code>) needs a menubar:</p>
  
  <pre><code>// Pseudo-code
  JMenuBar menuBar = new JMenuBar()
  
  JMenu fileMenu = new JMenu('File')
  
  JMenu manageMenu = new JMenu('Manage')
  JMenuItem widgetsSubmenu = new JMenuItem('Widgets')
  manageMenu.add(widgetsSubmenu)
  
  menuBar.add(fileMenu)
  menuBar.add(manageMenu)
  
  return menuBar
  </code></pre>
  
  <p>And your menu items need action listeners, many of which will updated your app's <code>contentPane</code>:</p>
  
  <pre><code>widgetsSubmenu.addActionListener(new ActionListener() {
      @Override
      void actionPerformed(ActionEvent actionEvent) {
          // Remove all the elements from the main contentPane
          yourApp.contentPane.removeAll()
  
          // Now add a new panel to the contentPane
          yourApp.contentPane.add(someNewPanel)
      }
  })
  </code></pre>
  
  <p><strong>And so there is intrinsically a circular dependency:</strong></p>
  
  <ol>
  <li>Your app/<code>JFrame</code> needs a <code>JMenuBar</code> instance</li>
  <li>Your <code>JMenuBar</code> needs 0+ <code>JMenus</code> and <code>JMenuItems</code></li>
  <li>Your <code>JMenuItems</code> (well, the ones that will update your <code>JFrame</code>'s <code>contentPane</code>') need action listeners</li>
  <li>And then, in order to update the <code>JFrame</code>'s <code>contentPane</code>, these action listeners need to reference your <code>JFrame</code></li>
  </ol>
  
  <p>Take the following module:</p>
  
  <pre><code>// Pseudo-code
  class MyModule extends AbstractModule {
      @Override
      void configure() {
          // ...
      }
  
      @Provides
      MyApp providesMyApp(JMenuBar menuBar) {
          // Remember MyApp extends JFrame...
          return new MyApp(menuBar, ...)
      }
  
      @Provides
      JMenuBar providesMenuBar(@Named('widgetsListener') ActionListener widgetsMenuActionListener) {
          JMenuBar menuBar = new JMenuBar()
  
          JMenu fileMenu = new JMenu('File')
  
          JMenu manageMenu = new JMenu('Manage')
          JMenuItem widgetsSubmenu = new JMenuItem('Widgets')
          widgetsSubmenu.addActionListener(widgetsMenuActionListener)
          manageMenu.add(widgetsSubmenu)
  
          menuBar.add(fileMenu)
          menuBar.add(manageMenu)
  
          return menuBar
      }
  
      @Provides
      @Named('widgetsListener')
      ActionListener providesWidgetsActionListener(Myapp myApp, @Named('widgetsPanel') JPanel widgetsPanel) {
          new ActionListener() {
              @Override
              void actionPerformed(ActionEvent actionEvent) {
                  // Here is the circular dependency. MyApp needs an instance of this listener to
                  // to be instantiated, but this listener depends on a MyApp instance in order to
                  // properly update the main content pane...
                  myApp.contentPane.removeAll()
                  myApp.contentPane.add(widgetsPanel)
              }
          }
      }
  }
  </code></pre>
  
  <p>This will produce circular dependency errors at runtime, such as:</p>
  
  <pre><code>Exception in thread "AWT-EventDispatcher" com.google.inject.ProvisionException: Unable to provision, see the following errors:
  
  1) Tried proxying com.me.myapp.MyApp to support a circular dependency, but it is not an interface.
      while locating com.me.myapp.MyApp
  </code></pre>
  
  <p>So I ask: <strong>what's the way of circumventing this?</strong> Does Guice have an API or extension library for dealing with this sort of problem? Is there a way to refactor the code to break the circular dependency? Some other solution?</p>
  
  <hr>
  
  <h3>Update</h3>
  
  <p>Please see my <a href="https://github.com/hotmeatballsoup/guice-swing-example" rel="noreferrer">guice-swing-example</a> project on GitHub for a SSCCE.</p>