tags:

views:

306

answers:

2

I would like a class analogous to spring's ContextLoader/ContextLoaderListener/ContextLoadServlet. These classes are invoked when the application server initializes and puts your configured context into memory.

What is the analogy of this for an application that does not have a container wrappering it?

This would preclude multiple instantiations, provide a unified retrieval location, and not suffer Double Checked Locking lameness either.

+1  A: 

The classic one is ClassPathXmlApplicationContext:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scripting.Messenger;

public final class Boot {

    public static void main(final String[] args) throws Exception {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("scripting/beans.xml");
        Messenger messenger = (Messenger) ctx.getBean("messenger");
        System.out.println(messenger);
    }
}

See more here

David Rabinowitz
AbstractApplicationContext doens't reallly offer much of anything that isn't an override of a function in ConfigurableApplicationContext or ApplicationContext. Generally there's no point to not either declaring ctx as the full concrete class or one of the interfaces. Also, you don't need to use the string array constructor if you're only passing one config file.
Jherico
I took the code snippet from the documentation (refered in my answer). Spring's ApplicationContecxt has more than a BeanFactory - Automatic BeanPostProcessor and BeanFactoryPostProcessor registration, convenient MessageSource access (for i18n) and ApplicationEvent publication. The documentation officially states "use an ApplicationContext unless you have a really good reason for not doing so". See more at http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#context-introduction-ctx-vs-beanfactory
David Rabinowitz
I've fixed the code example
David Rabinowitz
+2  A: 

An alternative solution can be found here:

http://stackoverflow.com/questions/707355/simple-spring-use-of-classpathapplicationcontext-for-standalone-apps-how-to-reu/707729#707729

for using SingletonBeanFactoryLocator.

Jon
thanks I knew there had to be a way that I wouldn't have to worry about the synchronization and caching.
Nathan Feger