Spring IoC container is heart of the spring framework. It is container to hold the spring objects and maintenance the spring objects lifecycle.
Spring provide two different type if containers.
- Bean Factory Container
- ApplicationContext Container
BeanFactory Container
Here is how to create a bean and use it in java class.
<bean id = "..." class = "..." />
InputStream is = new FileInputStream("beans.xml");
BeanFactory factory = new XmlBeanFactory(is);
//Get the bean from XML
MyClass obj = (MyClass) factory.getBean("myClassObj");
BeanFactory Methods.
- boolean containsBean(String)
- Object getBean(String)
- Object getBean(String, Class)
- Class getType(String name)
- boolean isSingleton(String)
- String[] getAliases(String)
ApplicationContext container
This is more towards enterprise implementations, It has many extensible feature like AOP, Databases, logging, etc. It contains all the functionality of BeanFactory as well. It’s an interface “org.springframework.context.ApplicationContext”;
Common implementation are,
- FileSystemXmlApplicationContext
- ClassPathXmlApplicationContext
- WebXmlApplicationContext
ApplicationContext appContext = new FileSystemXmlApplicationContext("beans.xml");
MyClass obj = (MyClass) context.getBean("myClassObj");