2012-04-04 117 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與豆。

你也可以結合這些方法。

相關問題