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.
–
–
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.
–
–
–
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 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.