How to implement ApplicationContext in Spring framework?

ApplicationContext is used when we are creating an enterprise-level application or web application. it is the main interface within a Spring application for providing configuration information to the application.

For Web Applications,we can register an ApplicationContext by using the ContextLoaderListener as following ways :-

				
					//register an ApplicationContext by using the ContextLoaderListener
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
				
			

There are following three ways we can implemented ApplicationContext in Spring.

FileSystemXmlApplicationContext : If we want to load the definitions of beans from an XML file then we will be use FileSystemXmlApplicationContext.The full path of XML bean configuration file is provided to the constructor.

				
					//provide XML based configuration metadata via the applicationContext.xml file
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
				
			

ClassPathXmlApplicationContext : When using ClassPathXmlApplicationContext, the container loads the bean definitions from the given xml file present in our CLASSPATH. Now that we have defined the Person bean in our application context.

				
					//load person bean from ClassPathXmlApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
Person obj = (Person) context.getBean("helloWorld");

// defined application context xml, we may use the classpath*: prefix at that times:
ApplicationContext context
    = new ClassPathXmlApplicationContext("classpath*:appContext.xml");
				
			

WebXmlApplicationContext : To provide configuration for a web application WebXmlApplicationContext is used. While the application is running, designed for web applications in Spring MVC. It’s a configuration file that informs Spring MVC how to manage controllers, view resolvers, locale resolvers, theme resolvers. WebApplicationContext is typically initialized inside the web.xml file.

Inside web.xml file :-

				
					<!-- here listener class bootstraps the root WebApplicationContext when the
  application starts and stores in the ServletContext.-->

<listener>
<listener-class>
   org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<!-- here DispatcherServlet creates its own WebApplicationContext.
This context defines beans specific web components and inherits all
bean definitions and resources from the root context. -->

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
   org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>