Gerald Nunn's Blog
« Portal's Flexible Framework Saves The Day | Main | Multilingual Documentum Content in WebLogic Portal »Page Flows and Spring Dependency Injection
Tuesday, February 07, 2006
Using Spring Dependency Injection with WebLogic Workshop Java Page Flows
I was at a client recently that wanted to use Spring Dependency Injection (DI) with Java Page Flows (JPF). Essentially they wanted to inject various service objects into JPF controllers in a way that was as seamless as possible. The challenge though is that JPF controllers are created by the framework and it is not currently possible to have Spring create them thus an alternative means must be found.
After some research we found that it was possible to DI an existing class with Spring using some methods from the autowiring facilities it provides. Specifically, there is a method called applyBeanPropertyValues in AutowireCapableBeanFactory that enables bean settings in Spring to be applied to an existing instance.
After searching the Spring support forums, I found this post (http://plainvanilla.typepad.com/spring/2005/06/configuring_arb.html) from one of the Interface 21 guys that shows how to access this interface from an existing ApplicationContext through the use of some judicious casting. Essentially it is as done as per the code below:
AutowireCapableBeanFactory factory = (AutowireCapableBeanFactory)((ConfigurableApplicationContext)context).getBeanFactory();
factory.applyBeanPropertyValues(this, this.getClass().getName());
Now that we know how to inject an existing class it is merely a matter of applying to the controller. You can choose to do this on an as needed basis or create a base page flow controller to centralize the logic and extend your existing controllers from this class.
While admittingly a perfect solution would involve no code change, this solution comes pretty close. If you opt to centralize the injection code into a base controller class, developers only need to make one change to support DI in their page flow controllers
Posted by Gerald Nunn at 4:02 PM | Categories: | Permalink
