Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I see that we have @org.springframework.context.annotation.ComponentScans and @org.springframework.context.annotation.ComponentScan .

  • How do we use @ComponentScans() ?
  • How is @ComponentScans() different from @ComponentScan({"com.org.abc", "com.org.xyz"})
  • ComponentScans can contain many nested ComponentScan, right? docs.spring.io/spring-framework/docs/current/javadoc-api/org/… vikingsteve Sep 20, 2018 at 9:38 @ComponentScans({@ComponentScan("com.org.abc"), @ComponentScan("com.org.xyz")}) is equivalent to your example . In the code of the ComponentScans annotation, there is nothing more than ComponentScan[] value(); Arnaud Sep 20, 2018 at 9:41 If you are using Java 8 you don't ever need to use ComponentScans . It's just a workaround for limitations of Java < 8 when having repeated annotations. Do not use it. Note that this was actually a pattern so there is plenty of annotations that have a plural version used in that way, but they can be safely ignored in Java8+ where you can simply repeat the annotations Giacomo Alzetta Sep 20, 2018 at 9:42

    @ComponentScan configures which packages to scan for classes with annotation configuration. We can specify the base package names directly with one of the basePackages or value arguments (value is an alias for basePackages)

    @Configuration
    @ComponentScan(basePackages = "com.baeldung.annotations")
    class VehicleFactoryConfig {}
      

    Also, we can point to classes in the base packages with the basePackageClasses argument:

    @Configuration
    @ComponentScan(basePackageClasses = VehicleFactoryConfig.class)
    class VehicleFactoryConfig {}
      

    Both arguments are arrays so that we can provide multiple packages for each.

    If no argument is specified, the scanning happens from the same package where the @ComponentScan annotated class is present.

    @ComponentScan leverages the Java 8 repeating annotations feature, which means we can mark a class with it multiple times:

    @Configuration
    @ComponentScan(basePackages = "com.baeldung.annotations")
    @ComponentScan(basePackageClasses = VehicleFactoryConfig.class)
    class VehicleFactoryConfig {}
      

    Alternatively, we can use @ComponentScans to specify multiple @ComponentScan configurations:

    @Configuration
    @ComponentScans({ 
        @ComponentScan(basePackages = "com.baeldung.annotations"), 
        @ComponentScan(basePackageClasses = VehicleFactoryConfig.class)
    class VehicleFactoryConfig {}
    

    You can found more Spring Bean Annotations

    ComponentScans that act as a container annotation that aggregates several ComponentScan annotations. So I think we no need to care it, just done it by your way. – Loc Le Sep 20, 2018 at 9:58 As @Giacomo said "If you are using Java 8 you don't ever need to use @ComponentScans(). It's just a workaround for limitations of Java < 8 when having repeated annotations. Do not use it. " – Abbin Varghese Sep 20, 2018 at 11:38

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.