1

AnnotationConfigWebApplicationContext the following when calling refresh() as is suggested in the javadoc

NoSuchBeanDefinitionException: No qualifying bean of type 'x' 

Javadoc for: AnnotationConfigWebApplicationContext#register

Note that AnnotationConfigWebApplicationContext.refresh() must be called in order for the context to fully process the new classes.

The bean x is defined in the rootContext. Without calling refresh there is no error and everything is autowired and starts correctly.

public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // Root context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(RootContext.class);
        rootContext.refresh();

        servletContext.addListener(new ContextLoaderListener(rootContext));

        // REST servlet
        AnnotationConfigWebApplicationContext restContext = new AnnotationConfigWebApplicationContext();
        restContext.register(RestConfig.class);
        restContext.refresh(); // <--------- Throws exception
        ServletRegistration.Dynamic restServlet = servletContext.addServlet("rest", new DispatcherServlet(restContext));
        restServlet.addMapping("/rest/");
    }
}

Am I missing something configuration wise?


EDIT:

RestConfig essentially looks like this.

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "foo.bar")
public class RestConfig extends WebMvcConfigurerAdapter {
   @Autowired
   private X x;
}

EDIT 2:

There's no evidence of using refresh() in the javadoc for WebApplicationInitializer that does contain a good example.

Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148

0 Answers0