java Spring MVC 和 Servlets 3.0 - 你还需要 web.xml 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15008126/
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
Spring MVC and Servlets 3.0 - Do you still need web.xml?
提问by Sotirios Delimanolis
In a typical Spring MVC web app, you would declare the DispatcherServlet
in web.xml
like so
在典型的 Spring MVC Web 应用程序中,您可以像这样声明DispatcherServlet
inweb.xml
<!-- MVC Servlet -->
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Along with listeners, filters, etc.
连同侦听器、过滤器等。
With servlet-api 3.0, you can declare your servlets with the annotation @WebServlet
instead of adding them to your web.xml
. Spring 3.2 already has @Configuration
and @EnableXYZ
for its context configuration. Does it have something similar for the DispatcherServlet
, ie. is there a way to configure your full Spring application without any xml?
使用 servlet-api 3.0,您可以使用注释声明您的 servlet,@WebServlet
而不是将它们添加到您的web.xml
. Spring 3.2 已经有了@Configuration
and @EnableXYZ
for its context 配置。是否有类似的东西DispatcherServlet
,即。有没有办法在没有任何 xml 的情况下配置完整的 Spring 应用程序?
回答by Alex
With JEE6, if your application container is Servlet 3.0 ready what you need to do is:
使用 JEE6,如果您的应用程序容器支持 Servlet 3.0,您需要做的是:
- Create a custom class that implements ServletContainerInitializer(i.e.
com.foo.FooServletContainer
) - Create a file in your
META-INF/services
folder namedjavax.servlet.ServletContainerInitializer
which will contain the name of your implementation above (com.foo.FooServletContainer
)
- 创建一个实现ServletContainerInitializer的自定义类(即
com.foo.FooServletContainer
) - 在您的
META-INF/services
文件夹中创建一个名为的文件,该文件javax.servlet.ServletContainerInitializer
将包含上述实现的名称 (com.foo.FooServletContainer
)
Spring 3 is bundled with a class named SpringServletContainerInitializer
that implements the stuff above (so you don't need to create yourself the file in META-INF/services
. This class just calls an implementation of WebApplicationInitializer
. So you just need to provide one class implementing it in your classpath (the following code is taken from the doc above).
春季3捆绑了一个名为类SpringServletContainerInitializer
实现以上(这样你就不会需要自己创建的文件中的东西META-INF/services
,这个类只是调用的实现WebApplicationInitializer
。所以,你只需要提供一个类在类路径中实现它(以下代码取自上面的文档)。
public class FooInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) {
WebApplicationContext appContext = ...;
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
That's it for the web.xml
thing, but you need to configure the webapp using @Configuration
, @EnableWebMvc
etc..
这是它的web.xml
事,但你需要使用配置Web应用程序@Configuration
,@EnableWebMvc
等等。
回答by Aniket Thakur
Yes you don't need web.xml
to startup your webapp Servlet 3.0+. As Alex already mentioned you can implement WebApplicationInitializer
class and override onStartup()
method. WebApplicationInitializer
is an interface provided by Spring MVC that ensures your implementation is detected and automatically used to initialize any Servlet 3 container.
是的,您不需要web.xml
启动您的 webapp Servlet 3.0+。正如亚历克斯已经提到的,您可以实现WebApplicationInitializer
类和覆盖onStartup()
方法。WebApplicationInitializer
是 Spring MVC 提供的接口,可确保检测到您的实现并自动用于初始化任何 Servlet 3 容器。
Is there a way to configure your full Spring application without any xml?
有没有办法在没有任何 xml 的情况下配置完整的 Spring 应用程序?
Adding this answer just to add another way. You don't need to implement WebApplicationInitializer
. An abstract base class implementation of WebApplicationInitializer
named AbstractDispatcherServletInitializer
makes it even easier to register the DispatcherServlet by simply overriding methods to specify the servlet mapping and the location of the DispatcherServlet configuration -
添加此答案只是为了添加另一种方式。您不需要实施WebApplicationInitializer
. WebApplicationInitializer
named的抽象基类实现AbstractDispatcherServletInitializer
使注册 DispatcherServlet 变得更加容易,只需覆盖方法来指定 servlet 映射和 DispatcherServlet 配置的位置 -
public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
@Override
protected WebApplicationContext createServletApplicationContext() {
XmlWebApplicationContext cxt = new XmlWebApplicationContext();
cxt.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
return cxt;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}