×
☰ See All Chapters

Spring @Component Annotation

@Component annotation is a class level annotation which indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning. Spring creates the beans for those classes annotated with @Component only when spring scans those classes. If a class is just annotated with @Component and if that class is not scanned by spring, you cannot get the bean from the context. Hence it is necessary that spring should scan the package having classes annotated with @Component annotation. So you should create AnnotationConfigApplicationContext by passing package name to constructor or register the package and refresh the context.

spring-component-annotation-0
 

If spring has not scanned the @Component annotated classes and using such bean will result in NoSuchBeanDefinitionException. We can also use @ComponentScan to specify packages for spring to scan for annotated components. You can read about @ComponentScan  annotation  in next chapter.

By default, the bean name will be same name as the class name with a lowercase initial. For the below class, bean name will be “employeeService

package com.java4coding;

 

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.stereotype.Component;

 

public class Demo {

        public static void main(String[] args) {

                AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.java4coding");

                EmployeeService employeeService = (EmployeeService) context.getBean("employeeService");

                employeeService.printEmployeeName("Manu Manjunatha");

                ((ConfigurableApplicationContext) context).close();

        }

}

 

@Component

class EmployeeService{

        public void printEmployeeName(String name){

                System.out.println(name);

        }

}

We can specify a different name to the bean by passing optional value argument of @Component annotation as below:

package com.java4coding;

 

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.stereotype.Component;

 

public class Demo {

        public static void main(String[] args) {

                AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.java4coding");

                EmployeeService employeeService = (EmployeeService) context.getBean("helloBean");

                employeeService.printEmployeeName("Manu Manjunatha");

                ((ConfigurableApplicationContext) context).close();

        }

}

 

@Component("helloBean")

class EmployeeService{

        public void printEmployeeName(String name){

                System.out.println(name);

        }

}

Spring @Component annotation example

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>Spring3_Component_Example</artifactId>

        <packaging>jar</packaging>

        <version>1.0-SNAPSHOT</version>

        <name>Spring3_Component_Example</name>

        <url>https://maven.apache.org</url>

        <properties>

                <org.springframework.version>3.2.0.RELEASE</org.springframework.version>

        </properties>

        <dependencies>

                <dependency>

                        <groupId>cglib</groupId>

                        <artifactId>cglib</artifactId>

                        <version>2.2.2</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-core</artifactId>

                        <version>${org.springframework.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-beans</artifactId>

                        <version>${org.springframework.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-context</artifactId>

                        <version>${org.springframework.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-context-support</artifactId>

                        <version>${org.springframework.version}</version>

                </dependency>

        </dependencies>

</project>

Employee.java

package com.java4coding;

 

import org.springframework.stereotype.Component;

 

@Component

public class Employee{

        public void printEmployeeName(String name){

                System.out.println(name);

        }

}

Demo.java

package com.java4coding;

 

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Demo {

        public static void main(String[] args) {

                AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.java4coding");

                Employee employee = (Employee) context.getBean("employee");

                employee.printEmployeeName("Manu Manjunatha");

                ((ConfigurableApplicationContext) context).close();

        }

}

 

Output:

spring-component-annotation-1
 

All Chapters
Author