As part of org.springframework.context package spring has provided an interface called ApplicationContext.
This interface inherits the properties of BeanFactory interface. Two classes provide the implementation of
ApplicationContext interface. They are
- ClassPathXmlApplicationContext
- FileSystemXmlApplicationContext
Points to ponder:
Question: What is the difference between BeanFactory and ApplicationContext?
Answer: ApplicationContext is much the same as a BeanFactory. Both ApplicationContext and BeanFactory load bean definitions, wire beans together and dispatch beans when requested. But since ApplicationContext is a complete superset of BeanFactory, all the capabilities and behavior of BeanFactory apply to ApplicationContext. In addition ApplicationContext has the below features over BeanFactory.
Answer: ApplicationContext is much the same as a BeanFactory. Both ApplicationContext and BeanFactory load bean definitions, wire beans together and dispatch beans when requested. But since ApplicationContext is a complete superset of BeanFactory, all the capabilities and behavior of BeanFactory apply to ApplicationContext. In addition ApplicationContext has the below features over BeanFactory.
- Application context support internationalization (I18N).
- Application context provides a generic way to load file resources, such as images.
- Application context instantiate bean when container is started (if scope is set to "Singleton"), it doesn't wait for getBean to be called.
Below is a simple example of Spring ClassPathXmlApplicationContext.
First create an Address bean under src as shown below.
File: com.ram.beans.Address.java
package com.ram.beans; public class Address { private String street; private String city; private String state; public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } }
Next create applicationContext.xml file under src. The file is given below.
<?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="addr" class="com.ram.beans.Address"> <property name="street" value="Richmond"></property> <property name="city" value="Melbourne"></property> <property name="state" value="Victoria"></property> </bean> </beans>
Next create a main class under src as shown.
File: com.ram.app.ClassPathXmlAppContextExample.java
package com.ram.app; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ram.beans.Address; public class ClassPathXmlAppContextExample { public static void main(String[] args) { ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml"); Address address = container.getBean("addr", Address.class); System.out.println("Street = "+address.getStreet()); System.out.println("City = "+address.getCity()); System.out.println("State = "+address.getState()); } }
No comments:
Post a Comment