Java Spring 4 - why i need manually scan for beans in WebApplicationInitializer

1.2k views Asked by At

I'm trying to setup a Web application with Spring 4.1 and Wicket 6.18. I want to use the full code approach. I have two test classes annotated with @Configuration and with @Bean. I want them to be discovered when i startup my app in Tomcat but it is not working unless i manually scan the base package in my custom WebApplicationInitialzer. By manually i mean to invoke AnnotationConfigWebApplicationContext.scan().

I looked through quite a few tutorials about the code based approach and didn't saw they do this. Even in the official spring docs they don't do this.

What i'm doing wrong that i need this and how to correct it?

My custom WebApplicationInitialzer looks like this:

public class WebAppInitializer implements WebApplicationInitializer {

    private static final Logger logger = LoggerFactory.getLogger(WebAppInitializer.class);

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        container.addListener(new ContextLoaderListener(context));

        context.scan("pl.myhouse"); //why do i need this???

        FilterRegistration filter = container.addFilter("wicket.myproject", WicketFilter.class);
        filter.setInitParameter("applicationClassName", WicketApplication.class.getName());
        filter.setInitParameter(WicketFilter.FILTER_MAPPING_PARAM, "/*");
        filter.addMappingForUrlPatterns(null, false, "/*");
    }

}
1

There are 1 answers

0
Robert Niestroj On BEST ANSWER

You both @The Head Rush and @Aeseir are correct. I was missing the rootContext.register(Appconfig.class) as well as the @ComponentScan. I even could share my knowledge further: Configuring Spring Wicket using Java Conf instead of xml :)