Skip to content

Spring, PicoContainer, Etc.

Randgalt edited this page Oct 26, 2012 · 5 revisions

Governator can be used in a Spring, PicoContainer or similar project in the following way:

Spring

Create a LifecycleListener that has access to your ApplicationContext

      LifecycleListener   lifecycleListener = new LifecycleListener()
      {
          @Override
          public void objectInjected(Object obj)
          {
              // context is an ApplicationContext
              context.getBeanFactory().registerSingleton(obj.getClass().getName(), obj);
          }

          @Override
          public void stateChanged(Object obj, LifecycleState newState)
          {
          }
      };

Bind a LifecycleListener:

See LifecycleListener for details.

You can then access Governator managed objects via:

  MyClass     m = context.getBeanFactory().getBean(MyClass.class);

PicoContainer

Create a LifecycleListener that has access to your MutablePicoContainer

      LifecycleListener   lifecycleListener = new LifecycleListener()
      {
          @Override
          public void objectInjected(Object obj)
          {
              // container is a MutablePicoContainer
              container.addComponent(obj);
          }

          @Override
          public void stateChanged(Object obj, LifecycleState newState)
          {
          }
      };

Bind a LifecycleListener:

See LifecycleListener for details.

You can then access Governator managed objects via:

  MyClass     m = container.getComponent(MyClass.class);