Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I keep on learning Spring and it is very difficult to figure out which implementation of ApplicationContext is intended for. I've standalone J2EE application and I don't interested in Web* or Portlet* implementations.

Can you provide me the brief list of possibilities (if isn't clear, see P.S. section of my question) and purposes of each implementation below:

  • ResourceAdapterApplicationContext
  • StaticApplicationContext
  • ClassPathXmlApplicationContext
  • FileSystemApplicationContext
  • A don't ask you to provide me reference to the docs. For example:

    ClassPathXmlApplicationContext Standalone XML application context, taking the context definition files from the class path, interpreting plain paths as class path resource names that include the package path

    But from that definition its not clear that ClassPathXmlApplicationContext also implements AbstractRefreshableApplicationContext and can be used to change beans definition without stopping server.

    No. Java + Spring IoC + communication via Websockets + Backbone.js to handle routing on client side. VB_ Jan 17, 2014 at 12:10 I am confused with the sentence "I am not interested in WEB or Portlet implementations" . Are you using Servlet API or you are working with web sockets directly? Pavel Horal Jan 17, 2014 at 12:13

    I'm sorry you don't want references to the docs, but that's where all the information is.

    StaticApplicationContext states

    org.springframework.context.ApplicationContext implementation which supports programmatic registration of beans and messages, rather than reading bean definitions from external onfiguration sources. Mainly useful for testing.

    So you use it to register bean definitions directly

    StaticApplicationContext context = new StaticApplicationContext();
    context.registerBeanDefinition(beanName, beanDefinition);
    

    This can be used in cases where your ApplicationContext needs to be dynamically changed. Note that you can pass a parent ApplicationContext to the StaticApplicationContext if you need both behaviors, ie. reading from XML/Java config and dynamically registering.

    ClassPathXmlApplicationContext is one of the more common ApplicationContext implementations in my opinion. You simply point it to an XML (bean definition) resource on the classpath and it loads it up. The javadoc states

    Useful for test harnesses as well as for application contexts embedded within JARs.

    You could therefore simply point to a resource on the classpath coming from a JAR and load that. It's simply enough to setup tests environments this way.

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("some-context.xml");
    // boom you're ready to go
    

    Note that Spring's JUnit support classes offer other (better) ways to setup testing environment.

    But from that definition its not clear that ClassPathXmlApplicationContext also implements AbstractRefreshableApplicationContext and can be used to change beans definition without stopping server.

    That's what the javadoc is for.

    FileSystemXmlApplicationContext is similar to the ClasspathXmlApplicationContext above, but it takes the configuration files from the file system instead of reading resources from the classpath.

    ResourceAdapterApplicationContext states

    org.springframework.context.ApplicationContext implementation for a JCA ResourceAdapter. Needs to be initialized with the JCA javax.resource.spi.BootstrapContext, passing it on to Spring-managed beans that implement BootstrapContextAware.

    I haven't worked with this one at all and I don't know where Resource Adapters are useful, but here are some more docs.

    Does that implementations have another interesting abbilities? (like dynamic config refresh) If yes, just re-count them. I ask because I found none. – VB_ Jan 17, 2014 at 15:22 Yes I mean that ClassPathXmlApplicationContext and FileSystemXmlApplicationContext both implements AbstractRefreshableApplicationContext and can dynamically add bean definitions. As for me it's very interesting (and in some cases useful) abbility. So I jusk asking if there are another such abilities in those 4 container implementations. – VB_ Jan 17, 2014 at 15:50 @VolodymyrBakhmatiuk If you have an IDE, open up the class you are interested in (you will need the source code) and look up the type hierarchy. You can see all the super types. Each type inherited provides its own set of functionality. There's too much to go over in an answer here. – Sotirios Delimanolis Jan 17, 2014 at 15:53

    Just to add couple things to @Solitirios answer:

    You forgot to mention several more context:

  • GenericApplicationContext
  • GenericXmlApplicationContext
  • AnnotationConfigApplicationContext
  • GenericWebApplicationContext
  • StaticWebApplicationContext
  • And many others.

    In general, GenericApplicationContext is almost the same as StaticApplicationContext, the only difference between them in MessageSource support in StaticApplicationContext. Purpose for both of these classes is for small tests with tiny application context with couple beans.

    GenericWebApplicationContext and StaticWebApplicationContext are also quite similar to each other, and typically they are used for emulation of Servlet container, e.g. tests or non-Servlet environment.

    F.e. you can use something like this in your code (f.e. tests):

    //create parent context
    ApplicationContext xmlContext = new GenericXmlApplicationContext("classpath:/spring-*.xml");
    //create mock servlet context
    MockServletContext mockServletContext = new MockServletContext();
    //create web context
    GenericWebApplicationContext webContext = new GenericWebApplicationContext(mockServletContext);
    //set attribute
    mockServletContext.setAttribute(GenericWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webContext);
    //set parent context
    webContext.setParent(xmlContext);
    //refresh context
    webContext.refresh();
    

    But there are couple contexts classes, which are worthy of attention. And considering your pre-requisites, I would choose one of them.

    GenericXmlApplicationContext is very good alternative of ClassPathXmlApplicationContext and FileSystemXmlApplicationContext. Consider this example:

    ApplicationContext context = new GenericXmlApplicationContext("classpath:some-context.xml");
    

    is equivalent to

    ApplicationContext context = new ClassPathXmlApplicationContext("some-context.xml");
    
    ApplicationContext context = new GenericXmlApplicationContext("some-context.xml");
    

    is equivalent to

    ApplicationContext context = new FileSystemXmlApplicationContext("some-context.xml");
    

    So GenericXmlApplicationContext looks more flexible.

    AnnotationConfigApplicationContext is a context holder, if you don't want to keep your beans in XML-file.

    //context creation
    ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
    //context class
        @Configuration
    @ComponentScan("com.examples.services")
    public class AppConfig {
        @Bean
        public DataSources dataSource() {
            DataSource ds = new BasicDataSource();
            //... init ds
            return ds;
    

    More information you can find here.

    thanks! And what is the difference between GenericApplicationContext and StaticApplicationContext – VB_ Jan 17, 2014 at 15:53 you said that Generic (Static) -WebApplicationContext are using to emulate Servlet container, e.g. non-Servlet environment. For what someone will want to do that. Explain that, please – VB_ Jan 17, 2014 at 16:20

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.