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)

@Configuration
public class SessionFactoryConfiguration {

   @Bean
   public DataSource dataSource() {
     DataSource dataSource = new OracleDataSource();
     dataSource.setURL();
     dataSource.setUser();
     return dataSource;
   }

   @Bean
   public SessionFactory sessionFactory() {
     SessionFactory sessionFactory
                = new SessionFactory();
     sessionFactory.setDataSource(dataSource());
     sessionFactory.setShowSql(true);
     return sessionFactory;
   }

}

 

Annnotation Based Configuration Metadata Example(Since Spring 2.5)

@Controller
public class JBTController {

	@Autowired
	com.dao.HibernateServiceImpl jbtService;

	@RequestMapping("/getAllJbtContact")
	public ModelAndView searchAllContacts(@RequestParam(required= false, defaultValue="") String name)
	{
		.....
		return mav;
	}
}

 

XML Based Configuration Metadata Example

<beans>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
	    <property name="dataSource" ref="dataSource1"/>
	    <property name="hibernateProperties">
	      <props>
	            <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>
	       </props>
	    </property>
		<property name="packagesToScan" value="com.beans"></property>
		<property name="mappingResources" value="JBT.hbm.xml"></property>
	</bean>

</beans>

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.

<beans>
<import resource="contact.xml"/>

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

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);

 

2 Comments Spring 3 IoC Container Overview

Leave 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.