java Spring MVC - DispatcherServlet 通过注解

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16707152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:43:33  来源:igfitidea点击:

Spring MVC - DispatcherServlet by annotations

javaspringmodel-view-controller

提问by user2160696

I got spring MVC application. It runs on Tomcat 7. By now i got this part in my web.xml file:

我得到了 spring MVC 应用程序。它在 Tomcat 7 上运行。现在我在我的 web.xml 文件中得到了这部分:

<servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/app-config.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

Is there any way to init it by annotations? I got a MainSettings.javaclass where all my beans are initialyzed by @Beanannotation. So how do i initDispatherServletthere?

有没有办法通过注释来初始化它?我得到了一个MainSettings.java类,我所有的 bean 都通过@Bean注释初始化。那么我如何在DispatherServlet那里初始化?

回答by Japan Trivedi

Here is the example with comments. Hope this helps you.

这是带有注释的示例。希望这对你有帮助。

public class ApplicationInitializer implements WebApplicationInitializer {

    //Called first when the application starts loading.
    public void onStartup(ServletContext servletContext)
            throws ServletException {
        System.out.println("Inside application initializer...");

        //Registering the class that incorporates the annotated DispatcherServlet configuration of spring
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(DispatcherConfig.class);

        //Adding the listener for the rootContext
        servletContext.addListener(new ContextLoaderListener(rootContext));

        //Registering the dispatcher servlet mappings.
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

}

回答by bunyaCloven

Writing this because Japs's answerleads to creation of another context, which doesn't see the contents of the security context.

写这个是因为Japs 的回答导致创建另一个上下文,它看不到安全上下文的内容。

public class WebInitializer extends
        AbstractAnnotationConfigDispatcherServletInitializer {

    private static final Log LOGGER = LogFactory.getLog(WebInitializer.class);

    @Override
    protected Class<?>[] getRootConfigClasses() {
        /*  this is where you will return you config class
         *  your root config class should @Import other configs 
         *  to make them work without needing them to add there
         */
        return new Class[] { ViewConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[0];
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}