Thursday 10, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 2 March 2014

Spring ApplicationContext Container

In this tutorial we are going to see Spring ApplicationContext container with example program.
ApplicationContext
The ApplicationContext container is similar to BeanFactory container.It can load bean definitions, wire beans together and dispense beans upon request.
The ApplicationContext is Interface which is defined in org.springframework.context.ApplicationContext. It adds more enterprise specific functionality such as the ability to resolve textual message from a property file and ability to publish application event to interested event listeners.
ApplicationContext includes all the functionality of BeanFactory.It is complete superset of BeanFactory.The Most commonly used Application context implementations are
  1. FileSystemXmlApplicationContext
  2. ClassPathXmlApplicationContext
  3. WebXmlApplicationContext.
We will see these topics in separate chapter.
The ApplicationContext Central interface to provide configuration for an application. This is read-only while the application is running, but may be reloaded if the implementation supports this.
An ApplicationContext provides:
    1. Bean factory methods for accessing application components. Inherited from ListableBeanFactory.
    2. The ability to load file resources in a generic fashion. Inherited from the ResourceLoader interface.
    3. The ability to publish events to registered listeners. Inherited from the  ApplicationEventPublisher  interface.
    4. The ability to resolve messages, supporting internationalization. Inherited from the MessageSource interface.
   5.  Inheritance from a parent context. Definitions in a descendant context will always take priority. This means, for example, that a single parent context can be used by an entire web application, while each servlet has its own child context that is independent of that of any other servlet.
Step by step procedure to create Spring Program using Eclipse given below.
Follow the Simple steps.
1. Select File –> New –> Java Project from your Eclipse IDE.
2.Create project called ApplicationContext.
3. Add the following jars into your build path.
commons-logging-1.1.3.jar
spring-beans-3.2.6.RELEASE.jar
spring-context-3.2.6.RELEASE.jar
spring-context-support-3.2.6.RELEASE.jar
spring-core-3.2.6.RELEASE.jar
spring-expression-3.2.6.RELEASE.jar
4. Now create package com.javatutorialscorner.spring under ApplicationContext Project
5. Next create SpringApplicationContext class under com.javatutorialscorner.spring package
SpringApplicationContext.java
01.package com.javatutorialscorner.spring;
02. 
03.public class SpringApplicationContext {
04. private String sayHello;
05. 
06. public void getSayHello() {
07.  System.out.println("Java Tutorials Corner " + sayHello);
08. }
09. 
10. public void setSayHello(String sayHello) {
11.  this.sayHello = sayHello;
12. }
13.}

6. Now Create RunApp class which contains Bean configuration path to load configuration from Bean.xml file

RunApp.java

01.package com.javatutorialscorner.spring;
02. 
03.import org.springframework.context.ApplicationContext;
04.import org.springframework.context.support.ClassPathXmlApplicationContext;
05. 
06.public class RunApp {
07. 
08. /**
09.  * @param args
10.  */
11. public static void main(String[] args) {
12.  // TODO Auto-generated method stub
13.   
14.  ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
15.  SpringApplicationContext applicationContext = (SpringApplicationContext)context.getBean("applicationContext");
16.  applicationContext.getSayHello();
17. }
18. 
19.}

In above program

First step I created Application context using ClassPathXmlApplicationContext  which load configuration from bean file  which located in class path of application, It take care of creating and initializing all the objects (Beans) declared in bean.xml

Next getBean() method used to get particular bean from created context. This method used bean name as parameter and returns generic object. We can caste to actual object.

Once Object Created you can access any method from that class.

7. Create Bean.xml at where your class files created (ClassPathXmlApplicationContext tries to load bean file from class path).

Bean.xml

01.<?xml version="1.0" encoding="UTF-8"?>
05. 
06.<bean id="applicationContext" class="com.javatutorialscorner.spring.SpringApplicationContext">
07.<property name="sayHello" value="say Hello"></property>
08.</bean>
09.</beans>

Bean.xml is default name given to bean configuration file. You can choose any name for your bean.xml but you can use file name in main application to create context and your file available in your class path must be same.

Bean.xml is used to assign unique ID to different beans and controls the creation object with different values, using above file you can pass any value to   variable sayHello without changing the class file.

Now you can run the program see the following output in console.

Java Tutorials Corner say Hello

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Spring singleton Bean ScopeIn this tutorial we are going to see Spring singleton Bean scope with example program.Only one shared instance of a singleton bean is managed, and all requests for beans with an id or ids matching that bean definition result … Read More
  • Spring Dependency InjectionIn this tutorial we are going to see about Spring Dependency Injection.Note : This tutorial created by referring Spring official web site. Dependencies A typical enterprise application does not consist of a single object (or … Read More
  • Spring DI – Constructor Argument ResolutionIn this tutorial we are going to see about Spring Constructor based Dependency Injection argument resolutions with example program.Constructor argument resolution matching occurs using the argument's type. If no potential amb… Read More
  • Spring Constructor-based dependency injectionIn this tutorial we are going to see about Spring Constructor-based dependency injection with example program.Constructor-based Dependency InjectionConstructor-based DI is accomplished by the container invoking a constructor … Read More
  • Spring Bean Scope In this tutorial we are going to see the overview of Spring bean scope. When you create bean definition in Spring, you can declare the scope of bean. You can control not only the various dependencies and configuration value… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Spring ApplicationContext Container Rating: 5 Reviewed By: eHowToNow