简体   繁体   English

web.xml配置作为注释

[英]web.xml configuration as annotations

I am trying to start a basic example project that uses Vaadin together with Spring. 我正在尝试启动一个将Vaadin与Spring结合使用的基本示例项目。 How can I put such xml configuration: 我该如何放置这样的xml配置:

 <context-param>
      <param-name>contextClass</param-name>
      <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      </param-value>
    </context-param>

   <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.app.config.AppConfig</param-value>
   </context-param>

   <listener>
     <listener-class>
       org.springframework.web.context.ContextLoaderListener
     </listener-class>
   </listener>

to such java based configuration: 到这样的基于Java的配置:

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class,       widgetset = "com.rrd.mavenproject1.AppWidgetSet")
public static class Servlet extends VaadinServlet {
}

You can't. 你不能 If you had a custom Servlet XxxListener , you could annotate it with @WebListener . 如果您有一个自定义的Servlet XxxListener ,则可以使用@WebListener对其进行@WebListener In this case you are trying to load ContextLoaderListener which you have no control over. 在这种情况下,您尝试加载无法控制的ContextLoaderListener

Context parameters are global to the application and therefore can't be added to one single @WebServlet annotation. 上下文参数对于应用程序是全局的,因此不能添加到单个@WebServlet批注中。

What you might be looking for is the Servlet API's ServletContainerInitializer or Spring's WebApplicationInitializer to perform Java configuration. 您可能正在寻找的是Servlet API的ServletContainerInitializer或Spring的WebApplicationInitializer来执行Java配置。

In either of these, you will have access to the ServletContext where you can add context parameters and listeners . 在这两种方法中,您都可以访问ServletContext ,在其中可以添加上下文参数侦听器 The strength of these classes is that you can create your Servlets, Filters, and Listeners from a Spring context. 这些类的优势在于您可以从Spring上下文中创建Servlet,过滤器和侦听器。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM