org.springframework.config.java.context
Class JavaConfigApplicationContext

java.lang.Object
  extended by org.springframework.core.io.DefaultResourceLoader
      extended by org.springframework.context.support.AbstractApplicationContext
          extended by org.springframework.context.support.AbstractRefreshableApplicationContext
              extended by org.springframework.config.java.context.JavaConfigApplicationContext
All Implemented Interfaces:
org.springframework.beans.factory.BeanFactory, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.HierarchicalBeanFactory, org.springframework.beans.factory.ListableBeanFactory, ConfigurableJavaConfigApplicationContext, org.springframework.config.java.internal.factory.TypeSafeBeanFactory, org.springframework.context.ApplicationContext, org.springframework.context.ApplicationEventPublisher, org.springframework.context.ConfigurableApplicationContext, org.springframework.context.Lifecycle, org.springframework.context.MessageSource, org.springframework.core.io.ResourceLoader, org.springframework.core.io.support.ResourcePatternResolver

public class JavaConfigApplicationContext
extends org.springframework.context.support.AbstractRefreshableApplicationContext
implements ConfigurableJavaConfigApplicationContext

Implementation of ApplicationContext that accepts @Configuration class literals and/or base package paths containing @Configuration classes. This feature enables users to avoid XML entirely and go "pure Java" with their configurations.

As with many ApplicationContext implementations, a number of constructors are provided, most of which cause a AbstractApplicationContext.refresh() immediately for convenience. Others, such as JavaConfigApplicationContext() do not, and leave the context "open for configuration" such that additional methods such as addConfigClass(Class), setParent(ApplicationContext), etc may be called. See individual constructors for more detail.

Author:
Chris Beams
See Also:
for use in web applications

Field Summary
 
Fields inherited from class org.springframework.context.support.AbstractApplicationContext
APPLICATION_EVENT_MULTICASTER_BEAN_NAME, logger, MESSAGE_SOURCE_BEAN_NAME
 
Fields inherited from interface org.springframework.context.ConfigurableApplicationContext
CONFIG_LOCATION_DELIMITERS, LOAD_TIME_WEAVER_BEAN_NAME
 
Fields inherited from interface org.springframework.beans.factory.BeanFactory
FACTORY_BEAN_PREFIX
 
Fields inherited from interface org.springframework.core.io.support.ResourcePatternResolver
CLASSPATH_ALL_URL_PREFIX
 
Fields inherited from interface org.springframework.core.io.ResourceLoader
CLASSPATH_URL_PREFIX
 
Constructor Summary
JavaConfigApplicationContext()
          TODO: JAVADOC
JavaConfigApplicationContext(org.springframework.context.ApplicationContext parent, java.lang.Class<?>... classes)
           
JavaConfigApplicationContext(java.lang.Class<?>... classes)
           
JavaConfigApplicationContext(java.lang.Class<?>[] classes, java.lang.String[] basePackages)
           
JavaConfigApplicationContext(JavaConfigApplicationContext parent)
          Create a new context with parent as the parent.
JavaConfigApplicationContext(java.lang.String... basePackages)
           
 
Method Summary
 void addBasePackage(java.lang.String basePackage)
          Allows for incrementally building up the base packages to be scanned by this context.
 void addConfigClass(java.lang.Class<?> cls)
          Allows for incrementally building up the configuration classes to be processed by this context.
protected  void finishRefresh()
           
<T> T
getBean(java.lang.Class<T> type)
          Return an instance of the given type.
<T> T
getBean(java.lang.Class<T> type, java.lang.String beanName)
          Return an instance named beanName and of type type.
 BeanNamingStrategy getBeanNamingStrategy()
           
protected  void invokeBeanFactoryPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
           
protected  void loadBeanDefinitions(org.springframework.beans.factory.support.DefaultListableBeanFactory beanFactory)
          Loads any specified @Configuration classes as bean definitions within this context's BeanFactory for later processing by.ConfigurationPostProcessor
 void setBeanNamingStrategy(BeanNamingStrategy namingStrategy)
           
 void setParent(org.springframework.context.ApplicationContext parent)
           
 
Methods inherited from class org.springframework.context.support.AbstractRefreshableApplicationContext
closeBeanFactory, createBeanFactory, customizeBeanFactory, getBeanFactory, hasBeanFactory, refreshBeanFactory, setAllowBeanDefinitionOverriding, setAllowCircularReferences
 
Methods inherited from class org.springframework.context.support.AbstractApplicationContext
addApplicationListener, addBeanFactoryPostProcessor, addListener, cancelRefresh, close, containsBean, containsBeanDefinition, containsLocalBean, destroy, destroyBeans, doClose, finishBeanFactoryInitialization, getAliases, getApplicationListeners, getAutowireCapableBeanFactory, getBean, getBean, getBean, getBeanDefinitionCount, getBeanDefinitionNames, getBeanFactoryPostProcessors, getBeanNamesForType, getBeanNamesForType, getBeansOfType, getBeansOfType, getDisplayName, getId, getInternalParentBeanFactory, getInternalParentMessageSource, getMessage, getMessage, getMessage, getParent, getParentBeanFactory, getResourcePatternResolver, getResources, getStartupDate, getType, initApplicationEventMulticaster, initMessageSource, isActive, isPrototype, isRunning, isSingleton, isTypeMatch, obtainFreshBeanFactory, onClose, onRefresh, postProcessBeanFactory, prepareBeanFactory, prepareRefresh, publishEvent, refresh, registerBeanPostProcessors, registerListeners, registerShutdownHook, setDisplayName, setId, start, stop, toString
 
Methods inherited from class org.springframework.core.io.DefaultResourceLoader
getClassLoader, getResource, getResourceByPath, setClassLoader
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.springframework.context.ConfigurableApplicationContext
addApplicationListener, addBeanFactoryPostProcessor, close, getBeanFactory, isActive, refresh, registerShutdownHook
 
Methods inherited from interface org.springframework.context.ApplicationContext
getAutowireCapableBeanFactory, getDisplayName, getId, getParent, getStartupDate
 
Methods inherited from interface org.springframework.beans.factory.ListableBeanFactory
containsBeanDefinition, getBeanDefinitionCount, getBeanDefinitionNames, getBeanNamesForType, getBeanNamesForType, getBeansOfType, getBeansOfType
 
Methods inherited from interface org.springframework.beans.factory.HierarchicalBeanFactory
containsLocalBean, getParentBeanFactory
 
Methods inherited from interface org.springframework.beans.factory.BeanFactory
containsBean, getAliases, getBean, getBean, getBean, getType, isPrototype, isSingleton, isTypeMatch
 
Methods inherited from interface org.springframework.context.MessageSource
getMessage, getMessage, getMessage
 
Methods inherited from interface org.springframework.context.ApplicationEventPublisher
publishEvent
 
Methods inherited from interface org.springframework.core.io.support.ResourcePatternResolver
getResources
 
Methods inherited from interface org.springframework.core.io.ResourceLoader
getClassLoader, getResource
 
Methods inherited from interface org.springframework.context.Lifecycle
isRunning, start, stop
 

Constructor Detail

JavaConfigApplicationContext

public JavaConfigApplicationContext()
TODO: JAVADOC


JavaConfigApplicationContext

public JavaConfigApplicationContext(java.lang.Class<?>... classes)

JavaConfigApplicationContext

public JavaConfigApplicationContext(java.lang.String... basePackages)

JavaConfigApplicationContext

public JavaConfigApplicationContext(JavaConfigApplicationContext parent)
Create a new context with parent as the parent. Requires an explicit call to refresh().

Parameters:
parent -

JavaConfigApplicationContext

public JavaConfigApplicationContext(java.lang.Class<?>[] classes,
                                    java.lang.String[] basePackages)

JavaConfigApplicationContext

public JavaConfigApplicationContext(org.springframework.context.ApplicationContext parent,
                                    java.lang.Class<?>... classes)
Method Detail

invokeBeanFactoryPostProcessors

protected void invokeBeanFactoryPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
Overrides:
invokeBeanFactoryPostProcessors in class org.springframework.context.support.AbstractApplicationContext

finishRefresh

protected void finishRefresh()
Overrides:
finishRefresh in class org.springframework.context.support.AbstractApplicationContext

loadBeanDefinitions

protected void loadBeanDefinitions(org.springframework.beans.factory.support.DefaultListableBeanFactory beanFactory)
                            throws java.io.IOException,
                                   org.springframework.beans.BeansException
Loads any specified @Configuration classes as bean definitions within this context's BeanFactory for later processing by.ConfigurationPostProcessor

Specified by:
loadBeanDefinitions in class org.springframework.context.support.AbstractRefreshableApplicationContext
Throws:
java.io.IOException
org.springframework.beans.BeansException
See Also:
JavaConfigApplicationContext(Class...), addConfigClass(Class)

setParent

public void setParent(org.springframework.context.ApplicationContext parent)
Specified by:
setParent in interface org.springframework.context.ConfigurableApplicationContext
Overrides:
setParent in class org.springframework.context.support.AbstractApplicationContext

setBeanNamingStrategy

public void setBeanNamingStrategy(BeanNamingStrategy namingStrategy)

getBeanNamingStrategy

public BeanNamingStrategy getBeanNamingStrategy()

addBasePackage

public void addBasePackage(java.lang.String basePackage)
Description copied from interface: ConfigurableJavaConfigApplicationContext
Allows for incrementally building up the base packages to be scanned by this context. Packages are presumed to have one or more Configuration class definitions within, but it is not considered an error if none are found. This method may only be called on a context still 'open for configuration' meaning that the user will need to manually call refresh() after all base packages have been added.

Specified by:
addBasePackage in interface ConfigurableJavaConfigApplicationContext
Parameters:
basePackage - a package to scan (recursively) for any classes annotated with Configuration

addConfigClass

public void addConfigClass(java.lang.Class<?> cls)
Description copied from interface: ConfigurableJavaConfigApplicationContext
Allows for incrementally building up the configuration classes to be processed by this context. May only be called on a context still 'open for configuration' meaning that the user will need to manually call refresh() after all classes have been added.

Specified by:
addConfigClass in interface ConfigurableJavaConfigApplicationContext
Parameters:
cls - a class annotated with Configuration

getBean

public <T> T getBean(java.lang.Class<T> type)
Description copied from interface: org.springframework.config.java.internal.factory.TypeSafeBeanFactory
Return an instance of the given type. If multiple instances of the same type exist, instances are inspected to see if exactly one is marked as Primary

Specified by:
getBean in interface org.springframework.config.java.internal.factory.TypeSafeBeanFactory
Parameters:
type - desired instance type
Returns:
instance matching type
See Also:
Primary

getBean

public <T> T getBean(java.lang.Class<T> type,
                     java.lang.String beanName)
Description copied from interface: org.springframework.config.java.internal.factory.TypeSafeBeanFactory
Return an instance named beanName and of type type. Useful in disambiguation cases where there is more than one bean of a given type within the factory and none is marked as Primary

This method is similar to its predecessor BeanFactory.getBean(String, Class), but this variant takes advantages of generics and removes the casting burden from the caller.

Specified by:
getBean in interface org.springframework.config.java.internal.factory.TypeSafeBeanFactory
Returns:
instance of type named beanName