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
contextConfigLocation
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
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 :-
org.springframework.web.context.ContextLoaderListener
dispatcher
org.springframework.web.servlet.DispatcherServlet