×
☰ See All Chapters

Elements/Attributes of @ComponentScan Annotation

In this tutorial you will learn to use different optional elements of @ComponentScan annotation.  Below are the list of optional attributes of @ComponentScan annotation.

  1. basePackages 

  2. basePackageClasses 

  3. useDefaultFilters 

  4. includeFilters 

  5. excludeFilters 

  6. lazyInit 

basePackages

basePackages is used to specify base packages to scan for annotated components. basePackages attribute has an alias name as “value”. So we can use “value” attribute in place of basePackages. The value for basePackages attribute should be an array of String, each String representing the package name.

package com.java4coding;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

 

@Configuration

@ComponentScan(basePackages = {"com.java4coding.packageA", "com.java4coding.packageD" })

public class SpringComponentScanAnnotationExample {

        public static void main(String[] args) {

                AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

                try {

                        ctx.register(SpringComponentScanAnnotationExample.class);

                        ctx.refresh();

                        System.out.println("classA1 available?: " + ctx.containsBean("classA1"));

                        System.out.println("classA2 available?: " + ctx.containsBean("classA2"));

                        System.out.println("classA3 available?: " + ctx.containsBean("classA3"));

                        System.out.println("classB1 available?: " + ctx.containsBean("classB1"));

                        System.out.println("classB2 available?: " + ctx.containsBean("classB2"));

                        System.out.println("classC available?: " + ctx.containsBean("classC"));

                        System.out.println("classD available?: " + ctx.containsBean("classD"));

                } finally {

                        ctx.close();

                }

        }

}

attributes-of-componentscan-0
basePackageClasses
 

We have to specify the Class objects of classes whose package are to be scanned. If class Demo is specified and if Demo class is present inside package com.java4coding.demo, then all the classes annotated with @Component @Repository, @Service, or @Controller inside the package com.java4coding.demo will be scanned.

package com.java4coding;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import com.java4coding.packageA.ClassA1;

import com.java4coding.packageB.ClassB2;

 

@Configuration

@ComponentScan(basePackages = { "com.java4coding.packageC", "com.java4coding.packageD" },

basePackageClasses = {ClassA1.class, ClassB2.class })

public class SpringComponentScanAnnotationExample {

        public static void main(String[] args) {

                AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

                try {

                        ctx.register(SpringComponentScanAnnotationExample.class);

                        ctx.refresh();

                        System.out.println("classA1 available?: " + ctx.containsBean("classA1"));

                        System.out.println("classA2 available?: " + ctx.containsBean("classA2"));

                        System.out.println("classA3 available?: " + ctx.containsBean("classA3"));

                        System.out.println("classB1 available?: " + ctx.containsBean("classB1"));

                        System.out.println("classB2 available?: " + ctx.containsBean("classB2"));

                        System.out.println("classC available?: " + ctx.containsBean("classC"));

                        System.out.println("classD available?: " + ctx.containsBean("classD"));

                } finally {

                        ctx.close();

                }

        }

}

 

attributes-of-componentscan-1


useDefaultFilters

When @ComponentScan is used without specifying packages, spring uses default filter and scan through all the similar package names of where @ComponentScan is used. useDefaultFilters indicates whether automatic detection of classes annotated with @Component @Repository, @Service, or @Controller should be enabled. By setting false to this value we can disable auto detection. When basePackages, basePackageClasses, excludeFilters, includeFilters are specified for @ComponentScan, then Spring will not auto scan for, meaning useDefaultFilters will get false automatically. But still we can set it to true if needed.

attributes-of-componentscan-2
 
attributes-of-componentscan-3
 

includeFilters and excludeFilters

Read our next chapter Spring @ComponentScan Filters - includeFilters, excludeFilters to learn about includeFilters and excludeFilters attributes of @ComponentScan.

lazyInit

Specify whether scanned beans should be registered for lazy initialization. Default is false; switch this to true when desired.


All Chapters
Author