|
From: Robert W. <rw...@gm...> - 2008-06-11 03:50:10
|
I just wanted to say thank you for your help, guiding me to the correct forum, and tolerance with my poorly worded problem. For those that may be facing a similar problem, I have reworded my question and posted it here http://forum.springframework.org/showthread.php?p=185719 Regards, Rob On Tue, Jun 10, 2008 at 8:25 PM, Chris Beams <chr...@sp...> wrote: > Robert, > > Have you considered ApplicationContextAware? > > > http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/context/ApplicationContextAware.html > > If this is not sufficient for your needs, please post your question below > to the community forums at > > http://forum.springframework.org > > You'll find this is a more responsive question-and-answer forum than this > mailing list which is currently low-traffic and used primarily for > announcements. > > - Chris > > > ----- Original Message ----- > From: "Robert Winch" <rw...@gm...> > To: spr...@li... > Sent: Tuesday, June 10, 2008 5:31:50 PM GMT -05:00 US/Canada Eastern > Subject: Re: [Springframework-developer] Using Spring as a Singleton > Factory? > > > Thank you for your quick response. I am attempting to easily get instances > out of the "current" ApplicationContext as a factory rather than using IoC. > A few outlines are given below. > > 1) Here is an example of what I am trying to avoid. I will comment on an > example below (perhaps I am doing this wrong and someone help with that). > > // I do not want them to have to know the resources to pass in. This > assumes it is locatorContext.xml, but that can change. > // This flexibility is externalized for a web application using the > contextConfigLocation parameter > BeanFactoryLocator locator = > ContextSingletonBeanFactoryLocator.getInstance("locatorContext.xml"); > > // Here I need to get a bean factory too? Why is it not just an > ApplicationContext that is returned? Are there benefits to this? > // If there are, I do not like that the user must know the id of the > factory and have that in the code (similar to the resource location, it > isn't externalized) > BeanFactoryReference factoryRef = locator.useBeanFactory("springFactory"); > > // Get an instance of the object. Here I am not concerned with the user > knowing the id of the bean. > Service service = (Service)factoryRef.getFactory().getBean("service"); > > Another thing I do not like is that there are so many steps involved in > getting the bean out of the applicationcontext. I just want to get an > instance. > > 2) One approach I have seen that is similar to what I want is outlined > below: > > // get the applicationcontext (but i do not want it to depend on j2ee. > // For example ContextLoader contains a compile time dependency on > ServletContext. > ApplicationContext context = > ContextLoader.getCurrentWebApplicationContext(); > > // get a bean from the applicationcontext > // once again, knowing the id of the bean is acceptable (and expected) > Service s = (Service) context.getBean("service"); > > // use the bean > > Is there any approach similar to #2 that works without a dependency on > j2ee? I am certain I could write some code to do this, but I would like to > do this with code that exists and in the cleanest way possible. Feedback on > even doing this is appreciated too. I would like to know that part of the > requirement is to use Spring as a Factory (Unfortunately, IoC is not > permissible for this use case). > > Thanks! > > > On Tue, Jun 10, 2008 at 2:03 PM, Chris Beams < > chr...@sp... > wrote: > > > Hi Robert, > > Please provide a bit more context about what you're trying to achieve > (a simple code example would suffice). > > - Chris > > -- > > Chris Beams > Senior Consultant, SpringSource > http://www.springsource.com > > > > > > On Jun 10, 2008, at 3:00 PM, Robert Winch wrote: > > > I am looking for a method to easily access the current > > ApplicationContext that is not bound to j2ee. The closest thing I > > have found is > org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext > > (), but this class has dependencies on j2ee. Is there a recommended > > way of obtaining the current ApplicationContext for applications > > that may exist outside the web? > > > > Thanks in advance! > > Rob > > ------------------------------------------------------------------------- > > Check out the new SourceForge.net Marketplace. > > It's the best place to buy or sell services for > > just about anything Open Source. > > > http://sourceforge.net/services/buy/index.php_______________________________________________ > > Springframework-developer mailing list > > Spr...@li... > > https://lists.sourceforge.net/lists/listinfo/springframework-developer > > > > > > ------------------------------------------------------------------------- > Check out the new SourceForge.net Marketplace. > It's the best place to buy or sell services for > just about anything Open Source. > http://sourceforge.net/services/buy/index.php > _______________________________________________ > Springframework-developer mailing list > Spr...@li... > https://lists.sourceforge.net/lists/listinfo/springframework-developer > > > ------------------------------------------------------------------------- > Check out the new SourceForge.net Marketplace. > It's the best place to buy or sell services for > just about anything Open Source. > http://sourceforge.net/services/buy/index.php > _______________________________________________ > Springframework-developer mailing list > Spr...@li... > https://lists.sourceforge.net/lists/listinfo/springframework-developer > > ------------------------------------------------------------------------- > Check out the new SourceForge.net Marketplace. > It's the best place to buy or sell services for > just about anything Open Source. > http://sourceforge.net/services/buy/index.php > _______________________________________________ > Springframework-developer mailing list > Spr...@li... > https://lists.sourceforge.net/lists/listinfo/springframework-developer > |