You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@beehive.apache.org by Niels Harremoes <nu...@pfa.dk> on 2007/02/07 09:30:55 UTC

Integrating netui with spring using FlowControllerFactory

Hi everyone!

We are using netui, but without beehive controllers. We would like to spring
inject resources into our pageflow controllers, and I were thinking of
extending the FlowControllerFactory to do this.

Have anybody done this previously?

I assume I need to override most/all methods to call super implementations,
and then spring inject any properties, probably using the
AutowireCapableBeanFactory.configureBean method.

However, this doesn't seem to cover the case where the PageFlow has been
serialized during session replication. My injected resources will most
likely be transient, since e.g. datasources doesn't really serialize well. 

But how/where should I re-inject them? Should I implement my own
HttpSessionActivationListener, or is there already a place in the framework
for this??

Any advice would be appreciated.
-- 
View this message in context: http://www.nabble.com/Integrating-netui-with-spring-using-FlowControllerFactory-tf3185579.html#a8841548
Sent from the Beehive - User mailing list archive at Nabble.com.


Re: Integrating netui with spring using FlowControllerFactory

Posted by Chad Schoettger <ch...@gmail.com>.
Hi Niels,

You're definitly on the right track - the FlowControllerFactory sounds
like the right place to do the injection when a page flow is created.

I'm not aware of any hooks in the framework you could use to re-inject
after serialization maybe someone on this list with a bit more
knowledge of page flows could confirm that.  Assuming there isn't -
implementing your own HttpSessionActivationListener sounds like the
way to go.

Once you have a solution, it would be great if you could describe your
approach on this list, I have seen a number of posts from Beehive
users interested in being able to use dependency injection.

 - Chad

On 2/7/07, Niels Harremoes <nu...@pfa.dk> wrote:
>
> Hi everyone!
>
> We are using netui, but without beehive controllers. We would like to spring
> inject resources into our pageflow controllers, and I were thinking of
> extending the FlowControllerFactory to do this.
>
> Have anybody done this previously?
>
> I assume I need to override most/all methods to call super implementations,
> and then spring inject any properties, probably using the
> AutowireCapableBeanFactory.configureBean method.
>
> However, this doesn't seem to cover the case where the PageFlow has been
> serialized during session replication. My injected resources will most
> likely be transient, since e.g. datasources doesn't really serialize well.
>
> But how/where should I re-inject them? Should I implement my own
> HttpSessionActivationListener, or is there already a place in the framework
> for this??
>
> Any advice would be appreciated.
> --
> View this message in context: http://www.nabble.com/Integrating-netui-with-spring-using-FlowControllerFactory-tf3185579.html#a8841548
> Sent from the Beehive - User mailing list archive at Nabble.com.
>
>

Re: Integrating netui with spring using FlowControllerFactory

Posted by Niels Harremoes <nu...@pfa.dk>.
OK, here's my first shot at an implementation. 
It doesn't handle serialization of FlowControllers during session
replication. And I haven't tested how it would work if the controller end up
getting wrapped/proxied by spring. It wires up controllers based on their
class name - this could possibly be improved?

// start SpringPageflowControllerFactory.java 
import org.apache.beehive.netui.pageflow.FlowController;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.context.ApplicationContext;

import javax.servlet.ServletContext;

/**
 * Override standard FlowControllerFactroy to spring inject properties in
new controllers
 */
public class SpringPageflowControllerFactory extends
org.apache.beehive.netui.pageflow.FlowControllerFactory {


  ApplicationContext applicationContext;

 /** Grab the spring web context - use
org.springframework.web.context.ContextLoaderListener to set this up */
@Override
  protected void reinit(ServletContext servletContext) {
    super.reinit(servletContext);
    applicationContext =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
  }


  /**
   * Allow Spring to configure the controller instantiated by the super
class 
   * @param controllerClass The desired class of the controller
   * @return A new controller which has been cofnigured by spring 
   * @throws InstantiationException if thrown by super implementation
   * @throws IllegalAccessException if thrown by super implementation
   */
  @Override
  public FlowController getFlowControllerInstance(Class controllerClass) 
                                   throws InstantiationException,
IllegalAccessException {
    FlowController flowController =
super.getFlowControllerInstance(controllerClass);

    // Now, spring inject properties based on the class
    String beanName = controllerClass.getName();
    if (applicationContext.containsBeanDefinition(beanName)) {
      log.debug("Wiring up controller " + beanName);
      AutowireCapableBeanFactory autowireCapableBeanFactory =
applicationContext.getAutowireCapableBeanFactory();
      flowController = (FlowController)
autowireCapableBeanFactory.configureBean(flowController, beanName);
    } else {
      log.debug("No bean definition found for bean " + beanName);
    }

    return flowController;
  }
}

// end SpringPageflowControllerFactory.java 

In myflow/Controller.java, I then add

  String testProperty = "It doesn't work";

  // Injected through spring
  public void setTestProperty(String testProperty) {
    this.testProperty = testProperty;
  }

And in my springWebContext.xml:

  <bean name="myflow.Controller" class="myflow.Controller" abstract="true">
    <property name="testProperty" value="It works"/>
  </bean>


-- 
View this message in context: http://www.nabble.com/Integrating-netui-with-spring-using-FlowControllerFactory-tf3185579.html#a8949475
Sent from the Beehive - User mailing list archive at Nabble.com.