[英]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.