Spring @ComponentScan
Spring @ComponentScan is an interface and it is part of the package "org.springframework.context.annotation". It will enable component scanning in the Spring application. Component scanning enables auto-detection of beans by Spring Container.
Java classes decorated with stereotypes such as @Configuration, @Component, @Service, @Controller, and @Repository are auto-detected by Spring. We can annotate component scan with or without arguments.
Either basePackageClasses() or basePackages() (or it's alias value()) may be specified to define the specific packages to scan.
@ComponentScan WITHOUT arguments tells Spring to scan the current package and all of its sub-packages. For example :
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
public class MovieApplication {
}
@ComponentScan WITH arguments tells Spring to scan the package mentioned with an attribute basePackages. For example:
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = "com.bsmlabs.microservices")
public class MovieApplication {
}
The @ComponentScan annotation is an alternative to <context:component-scan/> XML tag. It has an annotation-config attribute; however, this does not. Because in almost all cases when using @ComponentScan, default annotation config processing is assumed, for example, processing @Autowired.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan { }
Since ComponentScan's Retention Policy is Runtime, it will be executed during Runtime only. And it can appear in class-level or interface or enum declaration level. Hence its @Target is ElementType with Type i.e., @Target(ElementType.TYPE).
Attributes used with ComponentScan annotation:
1. value(): It is an aliasFor basePackages and it allows for more precise annotation declarations if no other attributes are needed, for example. @ComponentScan("com.bsmlabs.microservices") alternative to @ComponentScan(basepackages = "com.bsmlabs.microservices")
@AliasFor("basePackages")
String[] value() default {};
2. basePackages(): It describes to scan for annotated components, value is an alias this attribute. We can use basePackageClasses for a type-safe alternative to Spring-based package names.
@AliasFor("value")
String[] basePackages() default {};
For example:
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = "com.bsmlabs.microservices")
public class MovieApplication {
}
3. basePackageClasses(): It describes to scan all packages declared with annotated components. It means the package of each class specified will be scanned.
For example:
import org.springframework.context.annotation.ComponentScan;
@ComponentScan({"com.bsmlabs.microservices.movie", "com.bsmlabs.microservices.payment"})
public class MovieApplication {
}
4. nameGenerator(): The BeanNameGenerator class to be used for naming detected components within the Spring container i.e., ApplicationContext.
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
The default value of the BeanNameGenerator interface itself indicates that the scanner used to process this @ComponentScan annotation should use its inherited bean name generator.
Default AnnotationBeanNameGenerator or any custom instance supplied to the application context at startup or bootstrap time.
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FullyQualifiedAnnotationBeanNameGenerator;
@ComponentScan(nameGenerator = FullBeanNameGenerator.class)
public class MovieApplication {
}
5. scopeResolver: ScopeMetadataResolver is used for resolving the scope of detected components.
Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ScopedProxyMode;
@ComponentScan(basePackages = "com.bsmlabs.microservices", scopeResolver = MyResolverBean.class)
public class MovieApplication {
}
6. scopedProxy: Indicates whether proxies should be generated for detected components, which may be necessary when using scopes in a proxy-style fashion.
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ScopedProxyMode;
@ComponentScan(basePackages = "com.bsmlabs.microservices", scopedProxy = ScopedProxyMode.DEFAULT)
public class MovieApplication {
}
7. useDefaultFilters: It indicates whether automatic detection of classes annotated with @Component , @Controller, @Service, and @Repository. We can specify either false or true. default is true
boolean useDefaultFilters() default true;
For example:
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = "com.bsmlabs.microservices", useDefaultFilters = false)
public class MovieApplication {
}
8. includeFilters: It specifies which types are eligible for component scanning.
Filter[] includeFilters() default {};
For example:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
@ComponentScan(basePackages = "com.bsmlabs.microservices", includeFilters = @ComponentScan.Filter(type= FilterType.ANNOTATION) )
public class MovieApplication {
}
5 Types of filter available for ComponentScan.Filter
1. FilterType.ANNOTATION : Filter candidates marked with a given annotations. It is part of package org.springframework.core.type.filter.AnnotationTypeFilter
2. FilterType.ASSIGNABLE_TYPE: Filter candidates assignable to a given type. It is part of package org.springframework.core.type.filter.AssignableTypeFilter
3. FilterType.ASPECTJ: Filter candidates matches a given AspectJ type pattern expression. It is part of package org.springframework.core.type.filter.AspectJTypeFilter
4. FilterType.REGEX: Filter candidates matching a given regex pattern. It is part of package org.springframework.core.type.filter.RegexPatternTypeFilter
5. FilterType.CUSTOM: Filter candidates using a given custom org.springframework.core.type.filter.TypeFilter
9. excludeFilters: It specifies which types are not eligible for component scanning
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
@ComponentScan(basePackages = "com.bsmlabs.microservices", excludeFilters = @ComponentScan.Filter(type= FilterType.REGEX) )
public class MovieApplication {
}
10. lazyInit(): It specify whether scanned beans should be registered for lazy initiation
Thank you,
Mahendra
Comments