{"id":3942,"date":"2017-11-30T05:34:11","date_gmt":"2017-11-30T10:34:11","guid":{"rendered":"http:\/\/springframework.guru\/?p=3942"},"modified":"2019-06-05T02:06:50","modified_gmt":"2019-06-05T06:06:50","slug":"spring-component-scan","status":"publish","type":"post","link":"https:\/\/springframework.guru\/spring-component-scan\/","title":{"rendered":"Spring Component Scan"},"content":{"rendered":"<p>When developing Spring Boot applications, you need to tell the Spring Framework where to look for Spring components. Using component scan is one method of asking Spring to detect Spring managed components. Spring needs the information to locate and register all the Spring components with the application context when the application starts.<\/p>\n<p>Spring can auto scan, detect, and instantiate components from pre-defined project packages. It can auto scan all classes annotated with the stereotype annotations <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@Component<\/code>, <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@Controller<\/code>, <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@Service<\/code>, and <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@Repository<\/code><\/p>\n<p>In this post, I will discuss how Spring component scanning works.<\/p>\n<h2>Sample Application<\/h2>\n<p>Let us create a simple Spring Boot application to understand how component scanning works in Spring.<\/p>\n<p>We will start by writing few components.<\/p>\n<h5>DemoBeanA.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">\r\n   \/\/package guru.springframework.blog.componentscan.example.demopackageA;\r\n\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component(\"demoBeanA\")\r\npublic class DemoBeanA {\r\n}\r\n<\/pre>\n<h5>DemoBeanB1.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">\r\n   \/\/404: Not Found\r\n<\/pre>\n<h5>DemoBeanB2.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">\r\n   \/\/package guru.springframework.blog.componentscan.example.demopackageB;\r\n\r\nimport org.springframework.stereotype.Component;\r\n\r\n\r\n@Component(\"demoBeanB2\")\r\npublic class DemoBeanB2 extends DemoBeanB1{\r\n}\r\n<\/pre>\n<h5>DemoBeanB3.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">\r\n   \/\/package guru.springframework.blog.componentscan.example.demopackageB;\r\n\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component(\"demoBeanB3\")\r\npublic class DemoBeanB3 extends DemoBeanB2{\r\n}\r\n<\/pre>\n<h5>DemoBeanC.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">\r\n   \/\/package guru.springframework.blog.componentscan.example.demopackageC;\r\n\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component(\"demoBeanC\")\r\npublic class DemoBeanC {\r\n}\r\n<\/pre>\n<h5>DemoBeanD.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">package guru.springframework.blog.componentscan.example.demopackageD;\r\n\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component(\"demoBeanD\")\r\npublic class DemoBeanD {\r\n}<\/pre>\n<figure id=\"attachment_4223\" aria-describedby=\"caption-attachment-4223\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/bit.ly\/2yhpu6x\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4223 size-medium\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/ReactiveIsComing2NewSmall02-300x156.png\" alt=\"Spring Framework 5\" width=\"300\" height=\"156\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/ReactiveIsComing2NewSmall02-300x156.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/ReactiveIsComing2NewSmall02.png 560w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-4223\" class=\"wp-caption-text\">Click Here to Become a Spring Framework Guru!<\/figcaption><\/figure>\n<h2>The @SpringBootApplication Annotation<\/h2>\n<p>Spring needs to know which packages to scan for annotated components in order to add them to the IoC container. In a Spring Boot project, we typically set the main application class with the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@SpringBootApplication<\/code> annotation. Under the hood, <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@SpringBootApplication<\/code> is a composition of the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@Configuration<\/code>, <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\"> @ComponentScan<\/code>, and <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@EnableAutoConfiguration<\/code> annotations. With this default setting, Spring Boot will auto scan for components in the current package (containing the @SpringBoot main class) and its sub packages.<\/p>\n<p>To know more about these annotations, go through my <a href=\"http:\/\/springframework.guru\/spring-framework-annotations\/\" target=\"_blank\" rel=\"noopener noreferrer\">Spring Framework Annotations<\/a> post.<\/p>\n<p>Note: It is recommended that you locate your main application class in a root package above the component classes of the application.<\/p>\n<p>The code to create the main class and access components is this.<\/p>\n<h5>BlogPostsApplication.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">\r\n   \/\/package guru.springframework.blog;\r\n\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\nimport org.springframework.context.ApplicationContext;\r\n\r\n\r\n@SpringBootApplication\r\npublic class BlogPostsApplication {\r\n\r\n    public static void main(String[] args) {\r\n        ApplicationContext context = SpringApplication.run(BlogPostsApplication.class,args);\r\n            System.out.println(\"Contains A  \"+context.\r\n                    containsBeanDefinition(\"demoBeanA\"));\r\n            System.out.println(\"Contains B2  \" + context.\r\n                    containsBeanDefinition(\"demoBeanB2\"));\r\n            System.out.println(\"Contains C   \" + context.\r\n                    containsBeanDefinition(\"demoBeanC\"));\r\n\r\n\r\n    }\r\n}\r\n<\/pre>\n<p>The output of running the main class is this.<br \/>\n<a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplication.java_.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3963\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplication.java_-1024x152.png\" alt=\"Output of BlogPostsApplication.java class\" width=\"863\" height=\"128\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplication.java_-1024x152.png 1024w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplication.java_-300x45.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplication.java_-768x114.png 768w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplication.java_.png 1252w\" sizes=\"(max-width: 863px) 100vw, 863px\" \/><\/a><\/p>\n<p>As you can notice, all the classes in the sub packages of the main class <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">BlogPostsApplication <\/code> are auto scanned by Spring.<\/p>\n<h2>@ComponentScan \u2013 Identifying Base Packages<\/h2>\n<p>The <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@ComponentScan<\/code> annotation is used with the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@Configuration<\/code> annotation to tell Spring the packages to scan for annotated components. <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@ComponentScan<\/code> is also used to specify base packages and base package classes using <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">thebasePackageClasses<\/code> or <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">basePackages<\/code> attributes of <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@ComponentScan<\/code>.<\/p>\n<p>The <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">basePackageClasses <\/code> attribute is a type-safe alternative to <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">basePackages<\/code>. When you specify basePackageClasses, Spring will scan the package (and subpackages) of the classes you specify.<\/p>\n<p>A Java class annotated with <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@ComponentScan<\/code> with the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">basePackageClassesattribute<\/code> is this.<\/p>\n<h5>BlogPostsApplicationWithComponentScan.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">\r\n   \/\/package guru.springframework.blog;\r\nimport guru.springframework.blog.componentscan.example.demopackageB.DemoBeanB1;\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.annotation.ComponentScan;\r\nimport org.springframework.context.annotation.Configuration;\r\n\r\n@Configuration\r\n@ComponentScan(basePackages = {\"guru.springframework.blog.componentscan.example.demopackageA\",\r\n        \"guru.springframework.blog.componentscan.example.demopackageD\",\r\n        \"guru.springframework.blog.componentscan.example.demopackageE\"},\r\n        basePackageClasses = DemoBeanB1.class)\r\npublic class BlogPostsApplicationWithComponentScan {\r\n    public static void main(String[] args) {\r\n        ApplicationContext context = SpringApplication.\r\n                run(BlogPostsApplicationWithComponentScan.class,args);\r\n        System.out.println(\"Contains A  \"+context.\r\n                containsBeanDefinition(\"demoBeanA\"));\r\n        System.out.println(\"Contains B2  \" + context.\r\n                containsBeanDefinition(\"demoBeanB2\"));\r\n        System.out.println(\"Contains C   \" + context.\r\n                containsBeanDefinition(\"demoBeanC\"));\r\n        System.out.println(\"Contains D   \" + context.\r\n                containsBeanDefinition(\"demoBeanD\"));\r\n\r\n    }\r\n}\r\n<\/pre>\n<p>The output on running the main class is this.<br \/>\n<a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationWithComponentScan.java_.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3965\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationWithComponentScan.java_-1024x150.png\" alt=\"Output of BlogPostsApplicationWithComponentScan.java Class\" width=\"863\" height=\"126\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationWithComponentScan.java_-1024x150.png 1024w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationWithComponentScan.java_-300x44.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationWithComponentScan.java_-768x113.png 768w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationWithComponentScan.java_.png 1256w\" sizes=\"(max-width: 863px) 100vw, 863px\" \/><\/a><\/p>\n<p>The <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@ComponentScan<\/code> annotation uses the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">basePackages<\/code> attribute to specify three packages (and subpackages) that will be scanned by Spring. The annotation also uses the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">basePackageClasses<\/code> attribute to declare the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">DemoBeanB1<\/code> class, whose package Spring Boot should scan.<\/p>\n<p>As <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">demoBeanC<\/code> is in a different package, Spring did not find it during component scanning.<\/p>\n<h2>Component Scanning Filters<\/h2>\n<p>You can configure component scanning by using different type filters that Spring provides.<\/p>\n<p>By using filters, you can further narrow the set of candidate components from everything in basePackages to everything in the base packages that matches the given filter or filters.<\/p>\n<p>Filters can be of two types: include and exclude filters. As their names suggest, include filters specify which types are eligible for component scanning, while exclude filters specify which types are not.<\/p>\n<p>You can use the include and\/or exclude filters with or without the default filter. To disable the default filter, set the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">useDefaultFilters <\/code> element of the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@ComponentScan<\/code> annotation to a <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">false<\/code>.<\/p>\n<p>The code to disable the default filter is this.<\/p>\n<h5>BlogPostsApplicationDisablingDefaultFilters.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">\r\n   \/\/package guru.springframework.blog;\r\n\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.annotation.ComponentScan;\r\nimport org.springframework.context.annotation.Configuration;\r\n\r\n@Configuration\r\n@ComponentScan(value = \"guru.springframework.blog.componentscan.example.demopackageA\",\r\n        useDefaultFilters = false)\r\npublic class BlogPostsApplicationDisablingDefaultFilters {\r\n    public static void main(String[] args) {\r\n        ApplicationContext context = SpringApplication.\r\n                run(BlogPostsApplicationDisablingDefaultFilters.class,args);\r\n            System.out.println(\"Contains A  \" + context.containsBean(\"demoBeanA\"));\r\n    }\r\n}\r\n<\/pre>\n<p>In the preceding code, the value member defines the specific <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">guru.springframework.blog.componentscan.example.demopackageA<\/code> package to scan, while the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">useDefaultFilters<\/code> member disables the default filter.<\/p>\n<p>The output on running the main class is this.<br \/>\n<a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationDisablingDefaultFilters.java_.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3966\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationDisablingDefaultFilters.java_-1024x152.png\" alt=\"Output of BlogPostsApplicationDisablingDefaultFilters.java Class\" width=\"863\" height=\"128\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationDisablingDefaultFilters.java_-1024x152.png 1024w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationDisablingDefaultFilters.java_-300x45.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationDisablingDefaultFilters.java_-768x114.png 768w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationDisablingDefaultFilters.java_.png 1258w\" sizes=\"(max-width: 863px) 100vw, 863px\" \/><\/a><\/p>\n<p>As you can notice, the class <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">DemoBeanA <\/code> in the package <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">demopackageA <\/code> is unavailable when the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">useDefaultFilters <\/code> element of the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@ComponentScan<\/code> annotation is set to a false.<\/p>\n<h3>Component Scanning Filter Types<\/h3>\n<p>Spring provides the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">FilterType <\/code> enumeration for the type filters that may be used in conjunction with <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@ComponentScan<\/code>.<\/p>\n<p>The available <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">FilterType<\/code> values are:<\/p>\n<ul>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">FilterType.ANNOTATION<\/code>: Include or exclude those classes with a stereotype annotation<\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">FilterType.ASPECTJ<\/code>: Include or exclude classes using an AspectJ type pattern expression<\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">FilterType.ASSIGNABLE_TYPE<\/code>: Include or exclude classes that extend or implement this class or interface<\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">FilterType.REGEX<\/code>: Include or exclude classes using a regular expression<\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">FilterType.CUSTOM<\/code>: Include or exclude classes using a custom implementation of the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">org.springframework.core.type.TypeFilter<\/code> interface<\/li>\n<\/ul>\n<h3>Include Filters<\/h3>\n<p>With include filters, you can include certain classes to be scanned by Spring. To include assignable type, use the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">includeFilters <\/code> element of the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@ComponentScan<\/code> annotation with <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">FilterType. ASSIGNABLE_TYPE<\/code>. Using this filter, you can instruct Spring to scan for classes that extends or implements the class or interface you specify.<\/p>\n<p>The code to use the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">includeFilters <\/code> element of <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@ComponentScan<\/code> is this.<\/p>\n<h5>BlogPostsApplicationIncludeFilter.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">\r\n   \/\/package guru.springframework.blog;\r\n\r\nimport guru.springframework.blog.componentscan.example.demopackageB.DemoBeanB2;\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.annotation.ComponentScan;\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.context.annotation.FilterType;\r\n\r\n@Configuration\r\n@ComponentScan(basePackages = {\"guru.springframework.blog.componentscan.example.demopackageA\",\r\n        \"guru.springframework.blog.componentscan.example.demopackageB\"},\r\n        includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = DemoBeanB2.class),\r\n        useDefaultFilters = false)\r\npublic class BlogPostsApplicationIncludeFilter {\r\n    public static void main(String[] args) {\r\n        ApplicationContext context = SpringApplication.\r\n                run(BlogPostsApplicationIncludeFilter.class,args);\r\n            System.out.println(\"Contains A  \" + context.containsBean(\"demoBeanA\"));\r\n            System.out.println(\"Contains B1  \" + context.containsBean(\"demoBeanB1\"));\r\n            System.out.println(\"Contains B2  \" + context.containsBean(\"demoBeanB2\"));\r\n            System.out.println(\"Contains B3  \" + context.containsBean(\"demoBeanB3\"));\r\n    }\r\n}\r\n<\/pre>\n<p>The output on running the main class is this.<br \/>\n<a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationIncludeFilter.java_.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3967\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationIncludeFilter.java_-1024x198.png\" alt=\"Output of BlogPostsApplicationIncludeFilter.java Class\" width=\"863\" height=\"167\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationIncludeFilter.java_-1024x198.png 1024w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationIncludeFilter.java_-300x58.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationIncludeFilter.java_-768x149.png 768w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationIncludeFilter.java_.png 1358w\" sizes=\"(max-width: 863px) 100vw, 863px\" \/><\/a><\/p>\n<p>As shown in the preceding figure, Spring detected and used the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">demoBean3<\/code> component that extends <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">demoBean2<\/code>.<\/p>\n<figure id=\"attachment_4223\" aria-describedby=\"caption-attachment-4223\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/bit.ly\/2yhpu6x\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4223 size-medium\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/ReactiveIsComing2NewSmall02-300x156.png\" alt=\"Spring Framework 5\" width=\"300\" height=\"156\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/ReactiveIsComing2NewSmall02-300x156.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/ReactiveIsComing2NewSmall02.png 560w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-4223\" class=\"wp-caption-text\">Learn Spring Framework 5 with my Spring Framework 5: Beginner to Guru course!<\/figcaption><\/figure>\n<h3>Include Filters using Regex<\/h3>\n<p>You can use regular expressions to filter out components to be scanned by Spring. Use the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">includeFilters <\/code>nested annotation <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@ComponentScan.Filter<\/code> type <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">FilterType.REGEX <\/code>to set a pattern.<\/p>\n<p>The code to use an exclude filter based on regular expression is this.<\/p>\n<h5>BlogPostsApplicationFilterTypeRegex.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">\r\n   \/\/package guru.springframework.blog;\r\n\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.annotation.ComponentScan;\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.context.annotation.FilterType;\r\n\r\n@Configuration\r\n@ComponentScan(useDefaultFilters = false, includeFilters = @ComponentScan.Filter\r\n        (type = FilterType.REGEX, pattern = \".*[A2]\"))\r\npublic class BlogPostsApplicationFilterTypeRegex {\r\n    public static void main(String[] args) {\r\n        ApplicationContext context = SpringApplication.\r\n                run(BlogPostsApplicationFilterTypeRegex.class,args);\r\n            System.out.println(\"Contains A  \" + context.containsBean(\"demoBeanA\"));\r\n            System.out.println(\"Contains B1  \" + context.containsBean(\"demoBeanB1\"));\r\n            System.out.println(\"Contains B2  \" + context.containsBean(\"demoBeanB2\"));\r\n    }\r\n}\r\n<\/pre>\n<p>The output of the code snippet is this.<br \/>\n<a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationFilterTypeRegex.java_.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3968\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationFilterTypeRegex.java_-1024x164.png\" alt=\"Output of BlogPostsApplicationFilterTypeRegex.java Class\" width=\"863\" height=\"138\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationFilterTypeRegex.java_-1024x164.png 1024w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationFilterTypeRegex.java_-300x48.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationFilterTypeRegex.java_-768x123.png 768w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-BlogPostsApplicationFilterTypeRegex.java_.png 1357w\" sizes=\"(max-width: 863px) 100vw, 863px\" \/><\/a><br \/>\nAs shown in the preceding figure, the classes whose names end with A or 2 are detected by Spring.<\/p>\n<h3>Exclude Filters<\/h3>\n<p>The <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@ComponentScan<\/code> annotation enables you to exclude those classes that you do not want to scan.<\/p>\n<p>The code to use an exclude filter is this.<\/p>\n<h5>BlogPostsApplicationExcludeFilter.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">\r\n   \/\/package guru.springframework.blog;\r\n\r\nimport guru.springframework.blog.componentscan.example.demopackageB.DemoBeanB1;\r\nimport guru.springframework.blog.componentscan.example.demopackageB.DemoBeanB2;\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.annotation.ComponentScan;\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.context.annotation.FilterType;\r\n\r\n@Configuration\r\n@ComponentScan(basePackageClasses = {DemoBeanB1.class},\r\n        excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,\r\n                value = DemoBeanB2.class))\r\npublic class BlogPostsApplicationExcludeFilter {\r\n    public static void main(String[] args) {\r\n        ApplicationContext context = SpringApplication.\r\n                run(BlogPostsApplicationExcludeFilter.class,args);\r\n            System.out.println(\"Contains B1  \" + context.containsBean(\"demoBeanB1\"));\r\n            System.out.println(\"Contains B2  \" + context.containsBean(\"demoBeanB2\"));\r\n    }\r\n}\r\n<\/pre>\n<p>In this code, the nested annotation <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@ComponentScan.Filter<\/code> is used to specify the filter type as <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">FilterType.ASSIGNABLE_TYPE<\/code> and the base class that should be excluded from scanning.<\/p>\n<p>The output is this.<br \/>\n<a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-using-the-FilterType.ASSIGNABLE_TYPE-type.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3969\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-using-the-FilterType.ASSIGNABLE_TYPE-type-1024x152.png\" alt=\"Output of using the FilterType.ASSIGNABLE_TYPE type\" width=\"863\" height=\"128\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-using-the-FilterType.ASSIGNABLE_TYPE-type-1024x152.png 1024w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-using-the-FilterType.ASSIGNABLE_TYPE-type-300x45.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-using-the-FilterType.ASSIGNABLE_TYPE-type-768x114.png 768w, https:\/\/springframework.guru\/wp-content\/uploads\/2017\/11\/Output-of-using-the-FilterType.ASSIGNABLE_TYPE-type.png 1256w\" sizes=\"(max-width: 863px) 100vw, 863px\" \/><\/a><\/p>\n<p>As you can see, the class <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">DemoBeanB2 <\/code> has been excluded from being scanned.<\/p>\n<h2>Summary<\/h2>\n<p>The default auto scanning will work for your Spring Boot project most of the time. You only need to ensure that your <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@SpringBoot<\/code> main class is at the base package of your package hierarchy. Spring Boot will automatically perform a component scan in the package of the Spring Boot main class and below.<\/p>\n<p>One related annotation that I didn\u2019t mention in this post is <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@EntityScan<\/code> is more about JPA entity scanning rather than component scanning. Unlike <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@ComponentScan, the @EntityScan annotation <\/code>does not create beans. It only identifies which classes should be used by a specific persistence context.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When developing Spring Boot applications, you need to tell the Spring Framework where to look for Spring components. Using component scan is one method of asking Spring to detect Spring managed components. Spring needs the information to locate and register all the Spring components with the application context when the application starts. Spring can auto [&hellip;]<a href=\"https:\/\/springframework.guru\/spring-component-scan\/\" class=\"df-link-excerpt\">Continue reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":4575,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[21,104,34],"tags":[29,30],"class_list":["post-3942","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring","category-spring-boot","category-spring-mvc","tag-spring-boot","tag-spring-framework"],"jetpack_publicize_connections":[],"aioseo_notices":[],"modified_by":"Michelle Mesiona","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/NewBannerBOOTSWeb.jpg","jetpack_shortlink":"https:\/\/wp.me\/p5BZrZ-11A","_links":{"self":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/3942"}],"collection":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/comments?post=3942"}],"version-history":[{"count":11,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/3942\/revisions"}],"predecessor-version":[{"id":5394,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/3942\/revisions\/5394"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media\/4575"}],"wp:attachment":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media?parent=3942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/categories?post=3942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/tags?post=3942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}