Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring container implementations

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

P.S.

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.

like image 735
VB_ Avatar asked Dec 20 '22 19:12

VB_


1 Answers

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.

like image 141
Sotirios Delimanolis Avatar answered Jan 06 '23 20:01

Sotirios Delimanolis