web.xml WebApplicationInitializer 中的属性

web.xml properties in WebApplicationInitializer

有一种方法可以替代web.xml

public class Dispatcher implements WebApplicationInitializer{

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException{
        AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(ApplicationContext.class);
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

}

但如果使用 JNDI 连接,则必须粘贴到 web.xml 中:

<resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/cms</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
    </resource-ref> 

我们如何在 public void onStartup(ServletContext servletContext) throws ServletException{ ... } 方法中实现此 web.xml 属性 ??????

据我所知,在 Servlet 3 Java API 中没有 <resource-ref/> 的替代品,因此您必须使用 "web.xml"。对于大多数此类 API 缺陷,您可以在嵌入式容器的原生容器 APIs(Tomcat 等)中找到一些东西,但在这种情况下它没有任何意义这样做是因为重点是使用非嵌入式容器提供的东西。