Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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
java.lang.Exception:ServletConfig尚未初始化_Java_Spring_Servlets - Fatal编程技术网

java.lang.Exception:ServletConfig尚未初始化

java.lang.Exception:ServletConfig尚未初始化,java,spring,servlets,Java,Spring,Servlets,我有一个问题是: java.lang.Exception:ServletConfig尚未初始化 我找了将近两天,但我没有找到解决办法。每个人都说过 必须使用super.init(config)。我试过这个,但对我来说没有什么改变 我的init方法 @Override public void init(ServletConfig config) throws ServletException { super.init(config); AppServiceServlet ser

我有一个问题是:

java.lang.Exception:ServletConfig尚未初始化

我找了将近两天,但我没有找到解决办法。每个人都说过 必须使用super.init(config)。我试过这个,但对我来说没有什么改变

我的init方法

@Override
public void init(ServletConfig config) throws ServletException {

    super.init(config);

    AppServiceServlet service = new AppServiceServlet();
    try {
        service.getir();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    AutoCheckStatus.autoCheckStatus(600000);
}
和我的AppServiceServlet

    public List<SswAppServiceDto> getir() throws Exception {
    try {
        final WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this
                .getServletContext());
        setiAppServiceBusinessManager((IAppServiceBusinessManager) context.getBean(BEAN_ADI));

        List<SswAppService> result = getiAppServiceBusinessManager().getir();

        List<SswAppServiceDto> list = DtoConverter.convertSswAppServiceDto(result);

        for (int i = 0; i < result.size(); i++) {
            AppService appService = new AppService();
            appService.setServiceName(result.get(i).getName());
            appService.setUid(result.get(i).getServiceUid());
            appService.setHost(result.get(i).getHost());
            appService.setPort((int) result.get(i).getPort());
            SystemConfiguration.appServiceList.put(appService.getUid(), appService);
        }
        return list;

    } catch (RuntimeException e) {
        throw new Exception(e.getMessage(), e.getCause());
    }
}
在AppServiceServlet中,并显示:

java.lang.Exception:ServletConfig尚未初始化

请帮助。

此电话:

AppServiceServlet service = new AppServiceServlet();
通过
new
实例化servlet实例,这绕过了servlet的常规容器管理创建。因此,关键类变量(例如servlet配置)没有正确初始化

稍后,您将调用
getServletContext
,它只是重定向到
getServletConfig()


实际上,在servlet上以您现在的方式调用
new
是不符合规范的——servlet应该由web应用程序容器维护。启动启动servlet的正确方法是通过web.xml文件中的配置或注释。

请粘贴完整的StackTrace。将AppServiceServlet中的方法放入InitServlet,它就可以工作了。非常感谢你。你真是个了不起的人。@köksalfp你能描述一下你做了什么使这件事起作用吗?我正试图让它在一个Mockito测试用例中运行,并且遇到了同样的问题。@Marcus请确保调用
super.init(config)
在Servlet init方法中,配置中有什么?
AppServiceServlet service = new AppServiceServlet();