The Spring IoC container is at the core of the Spring Framework. The container will create the objects, wire them together, configure them, and manage their complete life cycle from creation till destruction. The Spring container uses dependency injection (DI) to manage the components that make up an application.
Spring provides following two distinct types of containers.
- BeanFactory container
- ApplicationContext container
1. Spring BeanFactory container
A BeanFactory is essentially nothing more than the interface for an advanced factory capable of maintaining a registry of different beans and their dependencies. The
BeanFactory enables you to read bean definitions and access them using the bean factory. When using just the
BeanFactory you would create one and read in some bean definitions in the XML format as follows:
InputStream is = new FileInputStream("beans.xml"); BeanFactory factory = new XmlBeanFactory(is); //Get bean HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");
Other ways to create bean factory are as below:
Resource resource = new FileSystemResource("beans.xml"); BeanFactory factory = new XmlBeanFactory(resource); ClassPathResource resource = new ClassPathResource("beans.xml"); BeanFactory factory = new XmlBeanFactory(resource);
Basically that’s all there is. Using
getBean(String), you can retrieve instances of your beans; the client-side view of the
BeanFactory is surprisingly simple.
BeanFactory interface has only six methods for client code to call:
boolean containsBean(String): returns true if the
BeanFactorycontains a bean definition or bean instance that matches the given name
Object getBean(String): returns an instance of the bean registered under the given name. Depending on how the bean was configured by the
BeanFactoryconfiguration, either a singleton and thus shared instance or a newly created bean will be returned. A
BeansExceptionwill be thrown when either the bean could not be found (in which case it’ll be a
NoSuchBeanDefinitionException), or an exception occurred while instantiating and preparing the bean
Object getBean(String, Class): returns a bean, registered under the given name. The bean returned will be cast to the given Class. If the bean could not be cast, corresponding exceptions will be thrown (
BeanNotOfRequiredTypeException). Furthermore, all rules of the
getBean(String)method apply (see above)
Class getType(String name): returns the
Classof the bean with the given name. If no bean corresponding to the given name could be found, a
NoSuchBeanDefinitionExceptionwill be thrown
boolean isSingleton(String): determines whether or not the bean definition or bean instance registered under the given name is a singleton. If no bean corresponding to the given name could be found, a
NoSuchBeanDefinitionExceptionwill be thrown
String getAliases(String): Return the aliases for the given bean name, if any were defined in the bean definition
2. Spring ApplicationContext container
ApplicationContext container adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners. This container is defined by the org.springframework.context.ApplicationContext interface.
The ApplicationContext container includes all functionality of the BeanFactory container, so it is generally recommended over the BeanFactory.
BeanFactory can still be used for lightweight applications like mobile devices or applet based applications where data volume and speed is significant.
The most commonly used
ApplicationContext implementations are:
- FileSystemXmlApplicationContext – This container loads the definitions of the beans from an XML file. Here you need to provide the full path of the XML bean configuration file to the constructor.
- ClassPathXmlApplicationContext – This container loads the definitions of the beans from an XML file. Here you do not need to provide the full path of the XML file but you need to set CLASSPATH properly because this container will look bean configuration XML file in CLASSPATH.
- WebXmlApplicationContext – This container loads the XML file with definitions of all beans from within a web application.
A sample code for application context instantiation will look like this.
ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
Drop me your questions in comments section.
Happy Leaning !!