2012-04-04 45 views
3

我有一个spring.xml文件,其中列出了所有bean定义,其中列出了所有使用bean,指定messageSource,dataSource等的依赖关系。另外,我有一个类ApplicationContext类,其中iam使用上下文获取所有豆。 的代码::在Spring MVC中使用ApplicationContext。

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)); 

    } 

} 

,我有其中一个ApplicationContext用于访问的JdbcTemplate相关Bean DAO类。我必须现在使用spring MVC开发一个web应用程序,并且我需要使用这个applicationContext。我如何在SpringMVC中使用这些applicationContext类。我knw我需要使用applicationcontextlisteners但写在哪里?谢谢..

回答

3

你有两种方法。在web.xml中定义这个。

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

并在您的WEB-INF文件夹中添加yourapp-servlet.xml以及beans和mvc配置。

其他方式是。在web.xml中定义这个。

<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> 

和你的WEB-INF添加的applicationContext.xml与豆。

你也可以结合这些方法。

相关问题