ApplicationContext in Spring

In previous we used Bean Factory container and discussed with example. Now we will discuss about the Application Context and Using with in Example.
Application Context Just like as Bean Factory‘s Big Brother with some additional functionality such as AOP concept, event notification and it adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners. This container is defined by the org.springframework.context.ApplicationContext interface.
The ApplicationContext includes all functionality of the BeanFactory, it is generally recommended over the BeanFactory. BeanFactory can still be used for light weight applications like mobile devices or applet based applications.
The most commonly used ApplicationContext implementations are:
  • FileSystemXmlApplicationContext: This container loads the definitions of the beans from an XML file. Here you need to provide the full path of the XML bean configuration file to the constructor.
  • FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("F:/my workspace/springAppDemo/src/spring.xml");

    Using wildcard for file system:

    FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("F:/*my workspace*/**/src/*-spring.xml");
  • ClassPathXmlApplicationContext This container loads the definitions of the beans from an XML file. Here you do not need to provide the full path of the XML file but you need to set CLASSPATH properly because this container will look bean configuration XML file in CLASSPATH.
  • ApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/dineshonjava/sdnext/springConfig/spring.xml");  

    Using wildcard for claspath:

    ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:com/dineshonjava/**/springConfig/spring.xml");
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:com/*/**/springConfig/*-spring.xml");
  • WebXmlApplicationContext: This container loads the XML file with definitions of all beans from within a web application.
  • ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
ApplicationContext in Spring

Let us see Example.

package com.sdnext.dineshonjava.applicationcontext.tutorial;

public class Triangle 
 private String type;
  * @param type the type to set
 public void setType(String type)
  this.type = type;

 public void draw()
  System.out.println(type+" Triangle Drawn");


<beans xmlns:aop="" xmlns:p="" xmlns:security="" xmlns:tx="" xmlns:xsi="" xmlns="" xsi:schemalocation="">
  <bean class="com.sdnext.applicationcontext.tutorial.Triangle" id="triangle">
    <property name="type" value="Equilateral"></property>

package com.sdnext.dineshonjava.applicationcontext.tutorial;

import org.springframework.context.ApplicationContext;

 * @author Dinesh Rajput
public class DrawingApp 

  * @param args
 public static void main(String[] args) 
  //Triangle triangle = new Triangle();
  //BeanFactory factory =new XmlBeanFactory(new FileSystemResource("spring.xml"));
 ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  Triangle triangle = (Triangle) context.getBean("triangle");

Now run the example:

Jun 18, 2012 11:40:04 AM prepareRefresh
INFO: Refreshing startup date [Mon Jun 18 11:40:04 IST 2012]; root of context hierarchy
Jun 18, 2012 11:40:04 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Jun 18, 2012 11:40:05 AM preInstantiateSingletons
INFO: Pre-instantiating singletons in defining beans [triangle]; root of factory hierarchy

Equilateral Triangle Drawn

Spring Related Topics you may like

  1. Spring Interview Questions and Answers
  2. Spring AOP Interview Questions and Answers
  3. Spring MVC Interview Questions
  4. Spring Security Interview Questions and Answers
  5. Spring REST Interview Questions and Answers
  6. Spring Boot Interview Questions and Answers
  7. Spring Boot Microservices Interview Questions and Answers
  8. Dependency Injection (DI) in Spring
  9. Spring IoC Container
  10. What is Bean Factory in Spring
  11. ApplicationContext in Spring
  12. Bean Autowiring in Spring
  13. Spring Bean Scopes
  14. Create Custom Bean Scope in Spring Example
  15. Using ApplicationContextAware in Spring
  16. Spring Bean Life Cycle and Callbacks
  17. BeanPostProcessor in Spring
  18. BeanFactoryPostProcessor in Spring
  19. Annotations in Spring and Based Configuration
  20. Spring JSR-250 Annotations
  21. JSR 330 Annotations in Spring
  22. Spring @Component, @Repository, @Service and @Controller Stereotype Annotations
  23. Method injection with Spring using Lookup method property
  24. Spring AOP-Introduction to Aspect Oriented Programming
  25. @Aspect Annotation in Spring
  26. Spring AOP AspectJ @Before Annotation Advice Example
  27. Spring AOP Before Advice Example using XML Config
  28. Spring AOP AspectJ @After Annotation Advice Example
  29. Spring AOP After Advice Example using XML Config
  30. Spring AOP AspectJ @AfterReturning Annotation Advice Example
  31. Spring AOP After-Returning Advice Example using XML Config
  32. Spring AOP AspectJ @AfterThrowing Annotation Advice Example
  33. Spring AOP After Throwing Advice Example using XML Config
  34. Spring AOP AspectJ @Around Annotation Advice Example
  35. Spring AOP Around Advice Example using XML Config
  36. Spring AOP Proxies in Spring
  37. Spring AOP Transaction Management in Hibernate
  38. Spring Transaction Management
  39. Spring Declarative Transaction Management Example
  40. Spring AOP-Ordering of Aspects with Example
  41. Spring Security Java Based Configuration with Example
  42. Spring Security XML Namespace Configuration Example

In Next Chapter we will discuss about Injecting of Constructor and Object in Application.
                                                    << Previous || Next >>

1 thought on “ApplicationContext in Spring”

Comments are closed.