Thursday, November 22, 2018
Home > Spring > Spring Application Context example

Spring Application Context example

In this tutorial, we are going to learn about the ApplicationContext interface. We will also find the difference between  BeanFactory interface and ApplicationContext interface.


What is ApplicationContext?

ApplicationContext is an advanced version of BeanFactory and it also has some great feature which helps us while developing any Spring application.

ApplicationContext is the central interface of the application. It is read-only at runtime, but can be reloaded if needed and is compatible with the application. Several classes implement the ApplicationContext interface, which allows a variety of configuration options and types of applications.


ApplicationContext provides the following features:

  • Bean Factory methods to access the components of the application.
  • The ability to load file resources generically.
  • The ability to publish events for registered earphones.
  • The ability to resolve messages to support internationalization.
  • inheritance of the main context.

Difference between  BeanFactory interface and ApplicationContext interface.

BeanFactoryApplicationContext
BeanFactory uses lazy initialization approachApplicationContext uses early initialization approach
BeanFactory creates a singleton bean only when it is requested from itApplicationContext creates all singleton beans at the time of its own initialization
Not supports internationalizationsupports internationalization


Difference between the implementation of BeanFactory and ApplicationContext

BeanFactory:

//Find xml file
Resource resource=new ClassPathResource("resources/spring.xml");
//Load xml into container
BeanFactory factory =new XmlBeanFactory(resource);
//Create Object of Test Class with reference of "t"
Object o=factory.getBean("h");
Hello he=(Hello)o;
he.hi();

ApplicationContext:

//Find xml file and load xml file into container as well
ApplicationContext ap=new ClassPathXmlApplicationContext("resources/spring.xml");
Hello he=(Hello)ap.getBean("h");
he.hi();

ApplicationContext example:

We are using Ecplise IDE and some required jars to develop below application.

ApplicationContext in Spring

Ecplise IDE : Ecplise Luna

Jars: Placed below jar in “Referenced Libraries” Folder

  1. commons-logging-1.2
  2. spring-beans-5.0.8.RELEASE
  3. spring-context-5.0.8.RELEASE
  4. spring-core-5.0.8.RELEASE

To create this example, we have created 3 files inside src directory

  1. Hello.java
  2. spring.xml
  3. client.java

Hello.java

package beans;
public class Hello
{
  private Hello() //Accept private constructor 
 {
    System.out.println("Hello Constructor");
 }
 public void hi()
 {
   System.out.println("Hi All!");
 }
}

 


spring.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
   <bean id="h" class="beans.Hello" scope="prototype"/>
</beans>

 


client.java

package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import beans.Hello;

public class Client 
{
  public static void main(String[] args) 
  {
     ApplicationContext ap=new ClassPathXmlApplicationContext("resources/spring.xml");
     Hello he=(Hello)ap.getBean("h");
     he.hi();
  }
}

 


OUTPUT
Hello Constructor
Hi All!
 

Related Topics:-


You may also like :-

Admin
We are group of application developer and admin.We are providing good content of information about IT technology and services .

Leave a Reply

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