简体   繁体   English

基于 Java 的配置在 Spring MVC 中究竟是如何工作的

[英]How exactly does Java-based configuration work in Spring MVC

I have no trouble in understanding how XML-based configuration works with Spring MVC.我很容易理解基于 XML 的配置如何与 Spring MVC 一起工作。 There is a standard file ("web.xml") in a standard directory ("WEB-INF/").标准目录(“WEB-INF/”)中有一个标准文件(“web.xml”)。 How does a server knows that this class is responsible for front-servlet initialization though?但是,服务器如何知道此类负责前端 servlet 初始化?

public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { RootApplicationContextConfig.class };
    }

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

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

Looking at this file, I can easily understand that a server will load the servlet on startup with the highest priority.查看这个文件,我可以很容易地理解服务器将在启动时以最高优先级加载 servlet。

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/dispatcher-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Since Servlet specification 3.0 you can use web.xml or annotations to configure servlets, and with the annotation based configuration additional mechanisms were included to make web.xml unnecessary.从 Servlet 规范 3.0 开始,您可以使用web.xml 或注解来配置 servlet,并且基于注解的配置包含了额外的机制以使web.xml变得不必要。 I'm not sure if there are cases where web.xml is still useful, I can't remember touching one in years.我不确定是否有web.xml仍然有用的情况,我不记得有多少年了。

SpringServletContainerInitializer class implements the javax.servlet.ServletContainerInitializer which is the starting point for the servlet container (eg Tomcat) to set things up. SpringServletContainerInitializer类实现了javax.servlet.ServletContainerInitializer ,它是 servlet 容器(例如 Tomcat)进行设置的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用基于Java的纯配置配置Spring MVC? - How to configure Spring MVC with pure Java-based configuration? 基于Java的Spring MVC Java配置无法看到类路径属性文件 - Spring MVC Java-based configuration can't see class-path properties file 为什么使用基于java的配置的Spring应用程序无法正常工作 - Why this Spring application with java-based configuration don't work properly 将基于Spring的Spring转换为基于Java的配置 - Convert Spring XML-based to Java-Based Configuration 将基于Java的配置转换为基于Spring的XML - Convert Java-Based Configuration to Spring XML-based 如何在基于Spring-boot的应用程序的测试中更改基于Java的配置? - How to change Java-based configuration in tests for Spring-boot based application? 基于Spring Java的配置加载资源(来自类路径) - Spring Java-based configuration loading resources (from classpath) 基于 Java 的 Spring 配置中具有多个构造函数的 Bean - Bean with multiple constructors in Java-based Spring configuration 在基于Java的spring配置层次结构中覆盖bean - Overriding beans in Java-based spring configuration hierarchy 在基于java的配置中覆盖xml定义的spring bean - Override xml-defined spring bean in java-based configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM