Spring provides following two types of containers.
BeanFactory
containerApplicationContext
containerThe Spring container uses dependency injection (DI) to manage the components that make up an application.
A bean factory is fine to simple applications, but to take advantage of the full power of the Spring framework, you may want to move up to Springs more advanced container, the application context.
On the surface, an application context is same as a bean factory.Both load bean definitions, wire beans together, and dispense beans upon request. But it also provides:
Normally a BeanFactory will load bean definitions stored in a configuration source (such as an XML document), and use the org.springframework.beans package to configure the beans. However, an implementation could simply return Java objects it creates as necessary directly in Java code. There are no constraints on how the definitions could be stored: LDAP, RDBMS, XML, properties file, etc. Implementations are encouraged to support references amongst beans (Dependency Injection).
In contrast to the methods in ListableBeanFactory, all of the operations in this interface will also check parent factories if this is a HierarchicalBeanFactory. If a bean is not found in this factory instance, the immediate parent factory will be asked. Beans in this factory instance are supposed to override beans of the same name in any parent factory.
Bean factory implementations should support the standard bean lifecycle interfaces as far as possible. The full set of initialization methods and their standard order is:
BeanNameAware's
setBeanNameBeanClassLoaderAware's
setBeanClassLoadeBeanFactoryAware's
setBeanFactoryEnvironmentAware's
setEnvironmentEmbeddedValueResolverAware's
setEmbeddedValueResolveResourceLoaderAware's
setResourceLoader (only applicable when running in an application context)ApplicationEventPublisherAware's
setApplicationEventPublisher (only applicable when running in an application context)MessageSourceAware's
setMessageSource (only applicable when running in an application context)ApplicationContextAware's
setApplicationContext (only applicable when running in an application context)ServletContextAware's
setServletContext (only applicable when running in a web application context)postProcessBeforeInitialization
methods of BeanPostProcessorsInitializingBean's
afterPropertiesSetOn shutdown of a bean factory, the following lifecycle methods apply:
postProcessBeforeDestruction methods of DestructionAwareBeanPostProcessors
DisposableBean's destroy
a custom destroy-method definition
The three commonly used implementation of ApplicationContext
are:
ClassPathXmlApplicationContext
It Loads context definition from an XML file located in the classpath, treating context definitions as classpath resources. The application context is loaded from the application’s classpath by using the code .ApplicationContext context = new
ClassPathXmlApplicationContext("bean.xml");
FileSystemXmlApplicationContext
: It loads context definition from an XML file in the filesystem. The application context is loaded from the file system by using the code .ApplicationContext context = new
FileSystemXmlApplicationContext("bean.xml");
XmlWebApplicationContext
: It loads context definition from an XML file contained within a web application.To Be Continue.....
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。