There is one way which can be used instead 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("/");
}
}
but if using JNDI connection, you have to paste into web.xml this one:
<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>
How can we implement this web.xml property in public void onStartup(ServletContext servletContext) throws ServletException{ ... }
method ?????
AFAIK there is no replacement for <resource-ref/>
in the Servlet 3 Java API, so you have to use a "web.xml". For the majority of such API deficiencies you could find something in the container native APIs (Tomcat etc.) for an embedded container, but in this case it doesn't make any sense to do that because the whole point is to consume something provided by a non-embedded container.