Interview Focused : What is ApplicationContext in Spring Framework?

ApplicationContext  in 10 Points.
1)  The ApplicationContext is the central interface within a Spring application for providing configuration information to the application. It is read-only at run time, but can be reloaded if necessary and supported by the application.
2) The ApplicationContext provides:
  • Bean factory methods for accessing application components.
  • The ability to load file resources in a generic fashion.
  • The ability to publish events to registered listeners.
  • The ability to resolve messages to support internationalization.
  • Inheritance from a parent context.
3) The ApplicationContext is a subclass of the BeanFactory interface so it has all the functionality a BeanFactory has and more. Unless you are writing an application that needs an extremely small memory footprint, BeanFactory shouldn’t be used directly.
4) 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.
  • 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.
  • WebXmlApplicationContext: This container loads the XML file with definitions of all beans from within a web application.
5) From Spring 2.5 onwards we can autowire the ApplicationContext as well as BeanFactory as below,
  1. private @Autowired BeanFactory beanFactory;
  2. private @Autowired ApplicationContext appContext;
6) A simple context file using XML
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="funnyBean"
  7. class="org.some.Fun">
  8. <property name="message" value="Have Fun." />
  9. </bean>
  10. </beans>

7) Using the above declared bean in your code,
  1. public static void main(String[] args) {
  2. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/application-context.xml");
  3. Fun funny = (Fun) applicationContext.getBean("FunnyBean");
  4. logger.debug("message='" + message.getMessage() + "'");
  5. }
8) In addition to standard BeanFactory lifecycle capabilities, ApplicationContext implementations detect and invoke ApplicationContextAware beans as well as ResourceLoaderAware, ApplicationEventPublisherAware and MessageSourceAware beans.
9) ApplicationContext will pre-instantiate all of the beans. So to lazy load beans use “Lazy-init” in Bean configuration.
10) ApplicationContext supports Annotation based dependency Injection. -@Autowired, @PreDestroy, which BeanFactory does not support.

Comments

Popular posts from this blog

Java Interview : Threads

Spring Framework Interview Notes : Part Two Wiring

Card Dealer In Java in Less than 5 minutes