Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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
Spring 如何在DispCacherServlet中重写contextConfigLocation_Spring_Tomcat_Servlets - Fatal编程技术网

Spring 如何在DispCacherServlet中重写contextConfigLocation

Spring 如何在DispCacherServlet中重写contextConfigLocation,spring,tomcat,servlets,Spring,Tomcat,Servlets,我想覆盖上下文配置位置 web.xml文件如下所示 <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>com.mypackage.MyDispacherServlet</servlet-class> <init-param> <param-name>contextConfigLocation&l

我想覆盖上下文配置位置

web.xml文件如下所示

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>com.mypackage.MyDispacherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:default-ctx.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
}

但是这个代码不起作用。
如何正确覆盖contextConfigLocation?

看起来您需要仔细查看尝试覆盖的
init
方法(在
HttpServletBean
中定义)

看起来
contextConfigLocation
参数是由
bw.setPropertyValues(pvs,true)设置的

不同的解决方案理念:

  • 您需要重写init方法complete(无需调用
    super.init()
    )。然后在
    bw.setPropertyValues(pvs,true)之前修改
    pvs
    (无论您如何操作)被调用

  • 或者在调用
    super.initServletBean()
    之前,重写
    initServletBean()
    并修改那里的属性

  • 这是我首先要尝试的: 或者您尝试覆盖
    getServletConfig()
    ,以便它返回您修改的配置


我可能遗漏了什么:我在init中获得了ServletConfig(但我无法更新它的属性);我已经扩展了org.springframework.web.servlet.DispatcherServlet,但没有这个。requiredProperties@Julias:这是因为我发布的代码不是解决方案,而是您试图覆盖的init方法的代码。如果查看此代码,并假设在执行
setContextConfigLocation(correctSpringXml)
后调用它(使用
super
),则它将再次覆盖配置因此,你需要找到另一种方法,要点是对其他方法的建议。谢谢你,我会寻找解决方案。我的问题是init()get是ServletConfig,我在调试器中看到了带有contextConfigLocation属性的映射,无法重写/更改它。@Julias请看一下我发布的代码。我希望你能理解为什么你的尝试不起作用!在你理解了这一点之后,继续阅读我写的关于如何解决问题的3种不同想法的部分。
public class MyDispacherServlet extends org.springframework.web.servlet.DispatcherServlet {


@Override
public void init(ServletConfig config) throws ServletException {
    // here will be code to find dynamically other-ctx.xml
    String correctSpringXml = "classpath*:other-ctx.xml";
    setContextConfigLocation(correctSpringXml) ;
    super.init(config);
}

@Override
protected WebApplicationContext initWebApplicationContext() throws BeansException {
    WebApplicationContext wac = super.initWebApplicationContext();

    return wac;
}
//unimportent parts removed
    @Override
    public final void init() throws ServletException {
            ...
        try {
            PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
            BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
            ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
            bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, this.environment));
            initBeanWrapper(bw);
            bw.setPropertyValues(pvs, true);
        }
        catch (BeansException ex) {...}
            ...
        // Let subclasses do whatever initialization they like.
        initServletBean();
            ...
    }