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.