Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Spring @Component Annotation with Example

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made the development of Web applications much easier than compared to classic Java frameworks and application programming interfaces (APIs), such as Java database connectivity (JDBC), JavaServer Pages(JSP), and Java Servlet. This framework uses various new techniques such as Aspect-Oriented Programming (AOP), Plain Old Java Object (POJO), and dependency injection (DI), to develop enterprise applications. Now talking about Spring Annotation

Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. 

There are many annotations available in Spring Framework. Some of the Spring Framework Annotations are listed below as follows where here we are going to discuss one of the most important annotations that are, @Component Annotation

  • @Required
  • @Autowired
  • @Configuration
  • @ComponentScan
  • @Bean
  • @Component
  • @Controller
  • @Service
  • @Repository, etc.

@Component Annotation

@Component is a class-level annotation. It is used to denote a class as a Component. We can use @Component across the application to mark the beans as Spring’s managed components. A component is responsible for some operations. Spring framework provides three other specific annotations to be used when marking a class as a Component.

  1. @Service
  2. @Repository
  3. @Controller

1: @Service: We specify a class with @Service to indicate that they’re holding the business logic. Besides being used in the service layer, there isn’t any other special use for this annotation. The utility classes can be marked as Service classes.

2: @Repository: We specify a class with @Repository to indicate that they’re dealing with CRUD operations, usually, it’s used with DAO (Data Access Object) or Repository implementations that deal with database tables.

3: @Controller: We specify a class with @Controller to indicate that they’re front controllers and responsible to handle user requests and return the appropriate response. It is mostly used with REST Web Services.

Note: All these four annotations are available in package ‘org.springframework.stereotype’ and part of ‘spring-context jar’.

Example: Spring @Component

Let’s create a very simple Spring boot application to showcase the use of Spring Component annotation and how Spring autodetects it with annotation-based configuration and classpath scanning.

Step 1: Create a Simple Spring Boot Project. 

Step 2: Add the spring-context dependency in your pom.xml file. Go to the pom.xml file inside your project and add the following spring-context dependency.



Step 3: Create a simple component class

Go to the src > main > java > your package name > right-click > New > Java Class and create your component class and mark it with @Component annotation. 



// Java Program to Illustrate Component class
package com.example.demo;
import org.springframework.stereotype.Component;
// Annotation
// Class
public class ComponentDemo {
    // Method
    public void demoFunction()
        // Print statement when method is called
        System.out.println("Hello GeeksForGeeks");

Step 4: Create an annotation-based spring context

Now go to your Application (@SpringBootApplication) file and here in this file create an annotation-based spring context and get the ComponentDemo bean from it.



// Java Program to Illustrate Application class
// Importing package here
package com.example.demo;
// Importing required classes
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
// Annotation
// Class
public class DemoApplication {
    // Main driver method
    public static void main(String[] args)
        // Annotation based spring context
        AnnotationConfigApplicationContext context
            = new AnnotationConfigApplicationContext();
        // Getting the Bean from the component class
        ComponentDemo componentDemo
            = context.getBean(ComponentDemo.class);
        // Closing the context
        // using close() method


So you can see the power of @Component annotation, we didn’t have to do anything to inject our component to spring context. The below image shows the directory structure of our Spring Component example project.

My Personal Notes arrow_drop_up
Last Updated : 29 Sep, 2022
Like Article
Save Article
Similar Reads
Related Tutorials