1

What is the difference between the following two statements though both are apparently used to load the xml configuration?

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

and

Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);

Is there any difference (wrt performance, usage) between these two?

Leo
  • 5,017
  • 6
  • 32
  • 55
  • Read the reference guide, which has a [section](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#context-introduction-ctx-vs-beanfactory) on this. The main difference lies in the `BeanFactory` and `ApplicationContext`. – M. Deinum Jan 06 '15 at 07:49
  • Note that *neither* of these is particularly good practice for some time now. Use Spring Boot for a standalone application, and either JavaConfig or `@ImportResource`. – chrylis -cautiouslyoptimistic- Jan 06 '15 at 07:51

1 Answers1

3

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 (e.g. "mypackage/myresource.txt"). Useful for test harnesses as well as for application contexts embedded within JARs. The config location defaults can be overridden via AbstractRefreshableConfigApplicationContext.getConfigLocations(), Config locations can either denote concrete files like "/myfiles/context.xml" or Ant-style patterns like "/myfiles/*-context.xml" (see the AntPathMatcher javadoc for pattern details).

Note: In case of multiple config locations, later bean definitions will override ones defined in earlier loaded files. This can be leveraged to deliberately override certain bean definitions via an extra XML file.

This is a simple, one-stop shop convenience ApplicationContext. Consider using the GenericApplicationContext class in combination with an XmlBeanDefinitionReader for more flexible context setup.

ClassPathResource

Resource implementation for class path resources. Uses either a given ClassLoader or a given Class for loading resources. Supports resolution as java.io.File if the class path resource resides in the file system, but not for resources in a JAR. Always supports resolution as URL.

Fore more details check the API http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/io/ClassPathResource.html

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/ClassPathXmlApplicationContext.html

Sudhakar
  • 3,104
  • 2
  • 27
  • 36