Hi Paul, Indeed the ModelClasses are newly instantiated on every request. I tried to tighten the relation of Spring and Magnolia (see: http://blog.orange11.nl/2012/05/31/spring-context-configuration-in-magnolia/), but making your beans available in your non-Spring contexts is really a convention that you will have to enforce.
I personally see a module's module class singleton as the place to expose your beans. What you can do is add the properties you want spring to wire in your module class with getters and setters, and then add the moduleClass to your Spring context with factoryMethod="getInstance": <bean class="nl.orange11.project.mymodule.MyModule" factory-method="getInstance"> <property name="serviceOne" ref="serviceOne" /> <property name="serviceTwo" ref="serviceTwo" /> <property name="anotherProperty" ref="anotherProperty" /> </bean> This way there is no Spring knowledge whatsoever in your moduleclass, and the properties are wired into your singeton. Let me know how you get along! Rgds, Erik On Tue, Aug 21, 2012 at 3:25 PM, Paul Shuttlewood (via Magnolia Forums) < [email protected]> wrote: > Hi, > > Have solved my problem and thought I'd pass on the knowledge here. > > Brief problem desc: Magnolia is instantiating my ModelClass via a new > command, but I want to Spring inject my data model/state/service classes > into the ModelClass instance. As Magnolia not Spring aware, all the > ModelClass dependencies end up null. > > Solution: first create a Spring Context provider class as described in > http://jerlinworld.wordpress.com/2009/07/20/making-your-spring-application-context-aware/ > > This will give you a static class that will be injected with the Spring > context. > > Then within ModelClass, use this provider class to get the Spring context, > and use getBean(type) to get each dependency. I lazy load these > dependencies and cache them locally as static references. So whenever > ModelClass is created by Magnolia (suspect this is on every page load that > is configured to use ModelClass - in my case, that is all my pages), its > dependencies are present. > > Note - I went down this approach as I have a simple Magnolia module > project (i.e. not a webapp module) that contains just my ModelClass, > templates (i.e. ftl files) and required Magnolia lifecycle classes. I also > have a seperate project that contains my data model classes and Spring > context. Both projects are packaged as jars and put in the WEB-INF/lib > folder so visible to Magnolia. Seems to work ok for me, and I'm happy with > this. > > Regards, > > Paul > > -- > Context is everything: > http://forum.magnolia-cms.com/forum/thread.html?threadId=5f4656e6-a1b8-47cd-870a-d4a564f3514f > > > ---------------------------------------------------------------- > For list details, see > http://www.magnolia-cms.com/community/mailing-lists.html > Alternatively, use our forums: http://forum.magnolia-cms.com/ > To unsubscribe, E-mail to: <[email protected]> > ---------------------------------------------------------------- > > ---------------------------------------------------------------- For list details, see http://www.magnolia-cms.com/community/mailing-lists.html Alternatively, use our forums: http://forum.magnolia-cms.com/ To unsubscribe, E-mail to: <[email protected]> ----------------------------------------------------------------
