Using ApplicationContext in Spring MVC.
I have a spring.xml file where all bean definitions are specified, where I have listed all dependencies using beans specified by messageSource, dataSource, etc. Also i have an ApplicationContext class where iam using context to get all beans. Code:
package models;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ApplicationContextClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AbstractApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml");
context.registerShutdownHook();
ATTModel attmodel = (ATTModel) context.getBean("att");
//ProjectModel project = (ProjectModel)context.getBean("project");
//project.call1();
attmodel.call();
System.out.println(context.getMessage("insertiondone",null, "Default greeting",null));
}
}
and I have a Dao class where applicationContext is used to access the JDBC related template bean. I need to develop a web application using spring MVC and I need to use this applicationContext. How can I use these applicationContext classes in SpringMVC. I need to use applicationcontextlisteners, but where do I write them? Thank..
source to share
You have two options. In web.xml, define this.
<servlet>
<servlet-name>yourapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
And in your WEB-INF folder add yourapp-servlet.xml with beans and mvc config.
Another way. In web.xml, define this.
<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>
And on your WEB-INF add applicationContext.xml with beans.
You can also combine these approaches.
source to share