Top Description Fields Constructors Methods
org.springframework.context.support

public Class ClassPathXmlApplicationContext

extends AbstractXmlApplicationContext
Class Inheritance
Imports
org.springframework.beans.BeansException, org.springframework.context.ApplicationContext, org.springframework.core.io.ClassPathResource, .Resource, org.springframework.lang.Nullable, org.springframework.util.Assert

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 getConfigLocations, Config locations can either denote concrete files like "/myfiles/context.xml" or Ant-style patterns like "/myfiles/*-context.xml" (see the org.springframework.util.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 org.springframework.beans.factory.xml.XmlBeanDefinitionReader for more flexible context setup.

Authors
Rod Johnson, Juergen Hoeller
See Also
getResource, getResourceByPath, GenericApplicationContext

Field Summary

Modifier and TypeField and Description
private Resource[]

Constructor Summary

AccessConstructor and Description
public
ClassPathXmlApplicationContext()

Create a new ClassPathXmlApplicationContext for bean-style configuration.

public
ClassPathXmlApplicationContext(ApplicationContext
the parent context
parent
)

Create a new ClassPathXmlApplicationContext for bean-style configuration.

public
ClassPathXmlApplicationContext(String
resource location
configLocation
)

Create a new ClassPathXmlApplicationContext, loading the definitions from the given XML file and automatically refreshing the context.

public
ClassPathXmlApplicationContext(String...
array of resource locations
configLocations
)

Create a new ClassPathXmlApplicationContext, loading the definitions from the given XML files and automatically refreshing the context.

public
ClassPathXmlApplicationContext(String[]
array of resource locations
configLocations
,
ApplicationContext
the parent context
parent
)

Create a new ClassPathXmlApplicationContext with the given parent, loading the definitions from the given XML files and automatically refreshing the context.

public
ClassPathXmlApplicationContext(String[]
array of resource locations
configLocations
,
boolean
whether to automatically refresh the context, loading all bean definitions and creating all singletons. Alternatively, call refresh manually after further configuring the context.
refresh
)

Create a new ClassPathXmlApplicationContext, loading the definitions from the given XML files.

public
ClassPathXmlApplicationContext(String[]
array of resource locations
configLocations
,
boolean
whether to automatically refresh the context, loading all bean definitions and creating all singletons. Alternatively, call refresh manually after further configuring the context.
refresh
,
ApplicationContext
the parent context
parent
)

Create a new ClassPathXmlApplicationContext with the given parent, loading the definitions from the given XML files.

public
ClassPathXmlApplicationContext(String
relative (or absolute) path within the class path
path
,
Class<?>
the class to load resources with (basis for the given paths)
clazz
)

Create a new ClassPathXmlApplicationContext, loading the definitions from the given XML file and automatically refreshing the context.

public
ClassPathXmlApplicationContext(String[]
array of relative (or absolute) paths within the class path
paths
,
Class<?>
the class to load resources with (basis for the given paths)
clazz
)

Create a new ClassPathXmlApplicationContext, loading the definitions from the given XML files and automatically refreshing the context.

public
ClassPathXmlApplicationContext(String[]
array of relative (or absolute) paths within the class path
paths
,
Class<?>
the class to load resources with (basis for the given paths)
clazz
,
ApplicationContext
the parent context
parent
)

Create a new ClassPathXmlApplicationContext with the given parent, loading the definitions from the given XML files and automatically refreshing the context.

Method Summary

Modifier and TypeMethod and Description
protected Resource[]
getConfigResources()

Overrides org.springframework.context.support.AbstractXmlApplicationContext.getConfigResources.

Return an array of Resource objects, referring to the XML bean definition files that this context should be built with.

Inherited from org.springframework.context.support.AbstractXmlApplicationContext:
initBeanDefinitionReaderloadBeanDefinitionsloadBeanDefinitionssetValidating

Field Detail

configResourcesback to summary
private Resource[] configResources
Annotations
@Nullable

Constructor Detail

ClassPathXmlApplicationContextback to summary
public ClassPathXmlApplicationContext()

Create a new ClassPathXmlApplicationContext for bean-style configuration.

See Also
setConfigLocation, setConfigLocations, afterPropertiesSet()
ClassPathXmlApplicationContextback to summary
public ClassPathXmlApplicationContext(ApplicationContext parent)

Create a new ClassPathXmlApplicationContext for bean-style configuration.

Parameters
parent:ApplicationContext

the parent context

See Also
setConfigLocation, setConfigLocations, afterPropertiesSet()
ClassPathXmlApplicationContextback to summary
public ClassPathXmlApplicationContext(String configLocation) throws BeansException

Create a new ClassPathXmlApplicationContext, loading the definitions from the given XML file and automatically refreshing the context.

Parameters
configLocation:String

resource location

Exceptions
BeansException:
if context creation failed
ClassPathXmlApplicationContextback to summary
public ClassPathXmlApplicationContext(String... configLocations) throws BeansException

Create a new ClassPathXmlApplicationContext, loading the definitions from the given XML files and automatically refreshing the context.

Parameters
configLocations:String[]

array of resource locations

Exceptions
BeansException:
if context creation failed
ClassPathXmlApplicationContextback to summary
public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException

Create a new ClassPathXmlApplicationContext with the given parent, loading the definitions from the given XML files and automatically refreshing the context.

Parameters
configLocations:String[]

array of resource locations

parent:ApplicationContext

the parent context

Exceptions
BeansException:
if context creation failed
ClassPathXmlApplicationContextback to summary
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException

Create a new ClassPathXmlApplicationContext, loading the definitions from the given XML files.

Parameters
configLocations:String[]

array of resource locations

refresh:boolean

whether to automatically refresh the context, loading all bean definitions and creating all singletons. Alternatively, call refresh manually after further configuring the context.

Exceptions
BeansException:
if context creation failed
See Also
refresh()
ClassPathXmlApplicationContextback to summary
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException

Create a new ClassPathXmlApplicationContext with the given parent, loading the definitions from the given XML files.

Parameters
configLocations:String[]

array of resource locations

refresh:boolean

whether to automatically refresh the context, loading all bean definitions and creating all singletons. Alternatively, call refresh manually after further configuring the context.

parent:ApplicationContext

the parent context

Exceptions
BeansException:
if context creation failed
See Also
refresh()
ClassPathXmlApplicationContextback to summary
public ClassPathXmlApplicationContext(String path, Class<?> clazz) throws BeansException

Create a new ClassPathXmlApplicationContext, loading the definitions from the given XML file and automatically refreshing the context.

This is a convenience method to load class path resources relative to a given Class. For full flexibility, consider using a GenericApplicationContext with an XmlBeanDefinitionReader and a ClassPathResource argument.

Parameters
path:String

relative (or absolute) path within the class path

clazz:Class<?>

the class to load resources with (basis for the given paths)

Exceptions
BeansException:
if context creation failed
See Also
org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class), org.springframework.context.support.GenericApplicationContext, org.springframework.beans.factory.xml.XmlBeanDefinitionReader
ClassPathXmlApplicationContextback to summary
public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz) throws BeansException

Create a new ClassPathXmlApplicationContext, loading the definitions from the given XML files and automatically refreshing the context.

Parameters
paths:String[]

array of relative (or absolute) paths within the class path

clazz:Class<?>

the class to load resources with (basis for the given paths)

Exceptions
BeansException:
if context creation failed
See Also
org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class), org.springframework.context.support.GenericApplicationContext, org.springframework.beans.factory.xml.XmlBeanDefinitionReader
ClassPathXmlApplicationContextback to summary
public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz, ApplicationContext parent) throws BeansException

Create a new ClassPathXmlApplicationContext with the given parent, loading the definitions from the given XML files and automatically refreshing the context.

Parameters
paths:String[]

array of relative (or absolute) paths within the class path

clazz:Class<?>

the class to load resources with (basis for the given paths)

parent:ApplicationContext

the parent context

Exceptions
BeansException:
if context creation failed
See Also
org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class), org.springframework.context.support.GenericApplicationContext, org.springframework.beans.factory.xml.XmlBeanDefinitionReader

Method Detail

getConfigResourcesback to summary
protected Resource[] getConfigResources()

Overrides org.springframework.context.support.AbstractXmlApplicationContext.getConfigResources.

Doc from org.springframework.context.support.AbstractXmlApplicationContext.getConfigResources.

Return an array of Resource objects, referring to the XML bean definition files that this context should be built with.

The default implementation returns null. Subclasses can override this to provide pre-built Resource objects rather than location Strings.

Returns:Resource[]

an array of Resource objects, or null if none

Annotations
@Override
@Nullable