Spring @Component and @ComponentScan annotation example

In this post under Spring Core, I will show with example the purpose and how to use “@Component” and “@ComponentScan” annotation together.

Till now in all my previous post, I have been defining a bean using “@Bean” annotation in an “@Configuration” annotated class as shown below

package package1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Example1 {
    @Bean
    public Bean1 bean1() {
        return new Bean1();
    }

    @Bean
    public Bean2 bean2() {
        return new Bean2();
    }

    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Example1.class);
        Bean1 bean1 = applicationContext.getBean("bean1", Bean1.class);
        System.out.println("Bean1: " + bean1);
        Bean2 bean2 = applicationContext.getBean("bean2", Bean2.class);
        System.out.println("Bean2: " + bean2);
    }
}

In the above code, we are manually defining the beans. This can be automated by using “@Component” annotation.

Once a class is annotated with “@Component” annotation, Spring creates a bean for it as shown below

Bean1

package package7;

import org.springframework.stereotype.Component;

@Component
public class Bean1 {
    @Override
    public String toString() {
        return "Hello its Bean1";
    }
}

Bean2

package package7;

import org.springframework.stereotype.Component;

@Component
public class Bean2 {
    @Override
    public String toString() {
        return "Hello its Bean2";
    }
}

In the above code, “Bean1” and “Bean2” classes are annotated with “@Component” annotation.

Spring goes through all the classes and check if they have any annotations applied and if the applied annotation is “@Component”. If yes it creates a bean for that class.

But first we have to tell Spring that we are not creating beans manually and it is Spring’s responsibility to scan for classes and create beans.

That’s where “@ComponentScan” annotation comes into picture. It is class level annotation.

Next we cannot tell Spring to scan whole hard disk searching for classes annotated with “@Component” annotation.

We need to specify exactly where Spring has to check for “@Component” annotated classes.

That’s where “@ComponentScan” annotation’s “basePackages” attribute come into picture.

Below is the main class that shows how to use both the “@ComponentScan” annotation and its attribute “basePackages”.

Main class


package package7;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = "package7")
public class Example7 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Example7.class);
        Bean1 bean1 = applicationContext.getBean("bean1", Bean1.class);
        System.out.println(bean1);
        Bean2 bean2 = applicationContext.getBean("bean2", Bean2.class);
        System.out.println(bean2);
    }
}

In the above main class, I have applied “@ComponentScan” annotation at the class level.

I have also used “basePackages” attribute and set it to package name under which we will have classes annotated with “@Component” annotation.

Spring will scan all the classes present under “package7” and creates beans.

In this way we use “@Component” and “@ComponentScan” annotations together.

Leave a Reply