Scroll to top

Spring 3 IoC Container Overview

In this tutorial we will check the overview of Spring IoC Container. Interface org.springframework.context.ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans. But how and from where container gets information like which Object to Instaniate, how to configure and assemble it.

Configuration metadata required for container can be provided in any of three ways.

  1. XML
  2. Java Code
  3. Java Annotations

Spring IoC container takes a form of configuration metadata(Which explain container how to initiate object and configure) and combine it with application classes which in the end produces fully configured system.

Spring IoC container is totally decoupled from the type in which metadata configuration is written(e.g. Java based, java annotation or XML).

Java Based metadata configuration Example(Since Spring 3.0)

public class SessionFactoryConfiguration {

   public DataSource dataSource() {
     DataSource dataSource = new OracleDataSource();
     return dataSource;

   public SessionFactory sessionFactory() {
     SessionFactory sessionFactory
                = new SessionFactory();
     return sessionFactory;



Annnotation Based Configuration Metadata Example(Since Spring 2.5)

public class JBTController {

	com.dao.HibernateServiceImpl jbtService;

	public ModelAndView searchAllContacts(@RequestParam(required= false, defaultValue="") String name)
		return mav;


XML Based Configuration Metadata Example

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
	    <property name="dataSource" ref="dataSource1"/>
	    <property name="hibernateProperties">
	            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
	            <prop key="hibernate.show_sql">true</prop>
	            <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
	            <prop key="hibernate.cache.use_second_level_cache">true</prop>
				<prop key="hibernate.cache.use_query_cache">true</prop>
		<property name="packagesToScan" value="com.beans"></property>
		<property name="mappingResources" value="JBT.hbm.xml"></property>


Instantiating IoC Container

ApplicationContext constructor requires string(configuration metadata).  Constructor will be used to instantiate the IoC container.

ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"contact.xml"});

contact.xml will contain configuration metadata as mentioned above.

ApplicationContext constructor loads bean definition from different XML configuration file. Different XML configuration file can be imported in a single XML file as below.

<import resource="contact.xml"/>

<bean id="bean1" class="..."/>

As mentioned above, external bean definitions are loaded from contact.xml.

Once configured as above, configured bean instance can be retrieved in below mentioned way.

ContactServiceImpl service = context.getBean("contact", ContactServiceImpl.class);


Author avatar


  1. Steve Johann

    Very nicely done, I like the clarity of the explanations.

    Thank you

  2. venu

    Nice explanations.
    Thank you

Post a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.