Open In App

Spring @Component Annotation with Example

Last Updated : 05 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 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 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: This Annotation specify that the class with @Service holds some business logic or services in it. Besides being used in the service layer, there isn’t any other special use for this annotation.

2: @Repository: This Annotation specifies that the class with @Repository is dealing with CRUD Operations( Create Read Update Delete). It mostly deals with DAO or with Repositories that deal with databases.

3: @Controller: This Annotation specifies that the class with @Controller to indicate that they’re front controllers and responsible to handle user requests and for returning the appropriate responses to user. It is mostly used with REST Web Services.

Note: ‘org.springframework.stereotype’ and part of ‘spring-context jar’ supports all the annotations mentioned above.

Spring @Component Example

Let us check Spring @Component Annotation with a simple basic example. In this example we will create a basic Spring boot application where we can check 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.

XML




<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.13</version>
</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. 

Example

Java




// Java Program to Illustrate Component class
package com.example.demo;
 
import org.springframework.stereotype.Component;
 
// Annotation
@Component
 
// 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.

Example

Java




// 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
@SpringBootApplication
 
// Class
public class DemoApplication {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Annotation based spring context
        AnnotationConfigApplicationContext context
            = new AnnotationConfigApplicationContext();
        context.scan("com.example.demo");
        context.refresh();
 
        // Getting the Bean from the component class
        ComponentDemo componentDemo
            = context.getBean(ComponentDemo.class);
        componentDemo.demoFunction();
 
        // Closing the context
        // using close() method
        context.close();
    }
}


Output:

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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads