I created a controller:

@RestController public class UserController { @RequestMapping(value = "/users",method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" }) @ResponseBody public List<User> getUser() { ApplicationContext context = new ClassPathXmlApplicationContext("SpringModule.xml"); UserDAO userDAO = (UserDAO) context.getBean("userDAO"); return userDAO.findAllUsers(); } } 

Also created ApplicationInitilizer :

 public class ApplicationInitilizer implements WebApplicationInitializer { private static final String DISPATCHER = "dispatcher"; public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(WebConfig.class); servletContext.addListener(new ContextLoaderListener(ctx)); ServletRegistration.Dynamic servlet = servletContext.addServlet(DISPATCHER, new DispatcherServlet(ctx)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); } } 

And the context SpringModule.xml :

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="database/SpringDatasource.xml"/> <import resource="dataBeans/SpringUser.xml"/> </beans> 

I run it in Tomcat, I go to http: // localhost: 8080 / users , I see in the log:

 28-Aug-2016 22:51:49.106 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory D:\Programs\Tomcat 9.0\webapps\manager has finished in 69 ms 28-Aug-2016 22:51:51.921 WARNING [http-apr-8080-exec-10] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/users] in DispatcherServlet with name 'dispatcher' 

If run as a simple application from the main class, everything works correctly. I am sure that the problem is with the fact that I incorrectly SpringModule.xml context SpringModule.xml . Tell me what's wrong.

    1 answer 1

    Spring supports several options for initializing the application - it is XML, Java and Groovy. You use the application configuration option via Java annotations. This line says this:

     AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); 

    If you were required to initialize the context from XML through WebApplicationInitializer , you need to:

     public class ApplicationInitializer implements WebwApplicationInitializer { @Override public void onStartup(ServletContext container) { XmlWebApplicationContext appContext = new XmlWebApplicationContext(); appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml"); ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(appContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } } 

    See all options in the documentation.

    XML config can be imported using the @ImportResource annotation.

     @Configuration @ImportResource({"/path/to/database/SpringDatasource.xml", "/path/to/dataBeans/SpringUser.xml"}) public class WebConfig extends WebMvcConfigurerAdapter { // ... } 

    In general, the XML configuration is convenient in cases when it is necessary to change the behavior of the application at runtime. If you do not need it, use the java configuration and annotations - then you do not need the XML configuration at all.

    You do not need to manually create an ApplicationContext in a JEE application. This code is superfluous in the controller.