0

How to Define ComponentScan Without Using annotation or Using XML in Spring? Is there any way to set componentScan using java code?

  • suppose you refer this [post](https://stackoverflow.com/questions/24883987/how-to-configure-component-scan-by-annotation-only-in-spring) – Rajith Pemabandu Jul 10 '17 at 01:06

1 Answers1

0

This blog says:

With the WebApplicationInitializer, Spring Framework Web MVC applications can now rid themselves of web.xml. Additional configuration/implementations are available for programmatically registering the DispatcherServlet and configuring the Spring container

You should be using something like this:

public class Initializer implements WebApplicationInitializer {

   public void onStartup(ServletContext servletContext)
        throws ServletException {
       AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();

       ctx.scan("com.package.name");
       //the rest of initialization
    }
 }
coderpc
  • 4,119
  • 6
  • 51
  • 93