Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between @ComponentScans and @ComponentScan?

Tags:

java

spring

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

  1. How do we use @ComponentScans() ?
  2. How is @ComponentScans() different from @ComponentScan({"com.org.abc", "com.org.xyz"})
like image 772
Abbin Varghese Avatar asked Sep 20 '18 09:09

Abbin Varghese


People also ask

What is difference between ComponentScan and ComponentScans?

ComponentScans Container annotation that aggregates several ComponentScan annotations. ComponentScan Configures component scanning directives for use with @Configuration classes.

What is the difference between @configuration and @ComponentScan?

3. How They Differ. The main difference between these annotations is that @ComponentScan scans for Spring components while @EnableAutoConfiguration is used for auto-configuring beans present in the classpath in Spring Boot applications.

When should I use ComponentScan?

The @ComponentScan annotation is used with the @Configuration annotation to tell Spring the packages to scan for annotated components. @ComponentScan also used to specify base packages and base package classes using thebasePackageClasses or basePackages attributes of @ComponentScan.

What is difference between @configuration and EnableAutoConfiguration?

@EnableAutoConfiguration : enable Spring Boot's auto-configuration mechanism. @ComponentScan : enable @Component scan on the package where the application is located (see the best practices) @Configuration : allow to register extra beans in the context or import additional configuration classes.


2 Answers

Spring can automatically scan a package for beans if component scanning is enabled.

@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

like image 142
Loc Le Avatar answered Sep 28 '22 13:09

Loc Le


Take a look at documentation:

ComponentScans Container annotation that aggregates several ComponentScan annotations.

ComponentScan Configures component scanning directives for use with @Configuration classes.

like image 21
Kamil W Avatar answered Sep 28 '22 11:09

Kamil W