Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SpringMVC中使用ApplicationContext。_Spring_Spring Mvc_Applicationcontext - Fatal编程技术网

在SpringMVC中使用ApplicationContext。

在SpringMVC中使用ApplicationContext。,spring,spring-mvc,applicationcontext,Spring,Spring Mvc,Applicationcontext,我有一个spring.xml文件,其中列出了所有bean定义,其中列出了使用bean的所有依赖项、指定的messageSource、dataSource等。我还有一个类ApplicationContext类,其中我使用上下文获取所有bean。 代码是: package models; import org.springframework.context.ApplicationContext; import org.springframework.context.support.Abstract

我有一个spring.xml文件,其中列出了所有bean定义,其中列出了使用bean的所有依赖项、指定的messageSource、dataSource等。我还有一个类ApplicationContext类,其中我使用上下文获取所有bean。 代码是:

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

    }

}

我有一个Dao类,其中applicationContext用于访问JDBCtemplate相关bean。我现在必须使用SpringMVC开发一个web应用程序,我需要使用这个applicationContext。如何在SpringMVC中使用这些applicationContext类。我知道我需要使用applicationcontextlisteners,但是在哪里编写它们呢?谢谢

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

<servlet>
    <servlet-name>yourapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<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>

你的应用程序
org.springframework.web.servlet.DispatcherServlet
在WEB-INF文件夹中添加带有bean和mvc配置的app-servlet.xml

另一种方式是。在web.xml中定义这个

<servlet>
    <servlet-name>yourapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<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
org.springframework.web.context.ContextLoaderListener
并向WEB-INF添加applicationContext.xml和bean

您还可以将这些方法结合起来