May 20 2015

Spring IoC Container and Beans

Spring IoC Container and Beans

Spring IoC Container

Spring IoC Container is the basis for spring framework. The primary job of spring IoC container is to instantiate the Java object, injecting its dependencies and managing them.

Spring Bean

The object created and managed by spring core container is called a Spring Bean. The bean objects has to be added in the configuration metadata which is taken by the Spring IoC Controller.

The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework IoC container.

BeanFactory

The org.springframework.beans.Bean.BeanFactory interface provides the end point for the spring core container to access the spring beans from container. BeanFactory provides mechanism can manage any type of object.

Resource resource=new ClassPathResource("applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(resource);

ApplicationContext

The org.springframework.context.ApplicationContext is a sub-interface of BeanFactory. It adds integration with Spring AOP features, message resource handling, event propagation and application layer specific context.

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  

The <bean> tag defines a spring bean and ‘id’ or ‘name’ is used to configure the identifier for the bean.

We can create BeanFactory or ApplicationContext object to initialize the spring core container. ‘getBean’ method of the Spring IoC container is used the get the spring bean with name or id.

Example

In the below example, EmployeeService is a simple java class and it is added in the spring configuration file which is passed to the Spring IoC container. ’empService’ is the identifier of the spring bean and Spring Ioc container will instantiate the employeeService object and manages it.

EmployeeService.java

package com.answersz.employee;

public class EmployeeService {

public void saveEmployee(String employeeName) {
 System.out.println("Saved : " + employeeName);
}

}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="empService" class="com.answersz.employee.EmployeeService" />

</beans>

ApplicationContext – ApplicationContextExample.java

package com.answersz.employee;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

// Spring IoC container and Beans - ApplicationContext
public class ApplicationContextExample {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext(
 "applicationContext.xml");

System.out.println("ApplicationContext IoC Container example");

EmployeeService employeeService = (EmployeeService) context
 .getBean("empService");

 employeeService.saveEmployee("answersz.com");
 }

}

Output

ApplicationContext IoC Container example
Saved : answersz.com

BeanFactory – BeanFactoryExample.java

package com.answersz.employee;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

//Spring IoC container and Beans - BeanFactory
public class BeanFactoryExample {

 public static void main(String[] args) {

 Resource resource = new ClassPathResource("applicationContext.xml");
 BeanFactory factory = new XmlBeanFactory(resource);

 System.out.println("BeanFactory IoC Container example");

 EmployeeService employeeService = (EmployeeService) factory
 .getBean("empService");

 employeeService.saveEmployee("answersz.com");

 }

}

Output

BeanFactory IoC Container example
Saved : answersz.com

Related articles :  Spring Setter InjectionConstructor Injection In Spring


Copyright © 2014. All rights reserved.

Posted May 20, 2015 by answersz in category Spring

Leave a Comment

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

*