Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring @Component Annotation with Example

The @Component annotation in Spring is a general-purpose stereotype annotation that indicates a class is a Spring component. When you annotate a class with @Component, it tells Spring that this class is a bean and should be automatically discovered and instantiated as a bean when component scanning is enabled.

There are specialized forms of the @Component annotation which are used for specific purposes, such as @Service, @Repository, and @Controller, but they all serve the fundamental purpose of class identification for component scanning.

Example:

  1. Creating a Simple Component

    Let's say you have a simple class that you want Spring to manage:

    package com.example;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class HelloWorld {
        public String sayHello() {
            return "Hello, World!";
        }
    }
    

    Here, the @Component annotation tells Spring to treat the HelloWorld class as a managed bean.

  2. Enabling Component Scanning

    If you're using XML-based configuration:

    <context:component-scan base-package="com.example"/>
    

    If you're using Java-based configuration:

    @Configuration
    @ComponentScan("com.example")
    public class AppConfig {
        // ...
    }
    
  3. Using the Component in another class

    Here's how you can use the HelloWorld component in another class using @Autowired:

    package com.example;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class GreetingService {
    
        private final HelloWorld helloWorld;
    
        @Autowired
        public GreetingService(HelloWorld helloWorld) {
            this.helloWorld = helloWorld;
        }
    
        public void greet() {
            System.out.println(helloWorld.sayHello());
        }
    }
    

    In the above example, Spring automatically injects the HelloWorld bean into the GreetingService bean due to the @Autowired annotation on the constructor.

  4. Bootstrap the Application

    Finally, you'd bootstrap your application, either through a main method using AnnotationConfigApplicationContext (for a standalone application) or through a web configuration (for a web application). For a standalone application:

    public class Application {
        public static void main(String[] args) {
            ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
            GreetingService service = context.getBean(GreetingService.class);
            service.greet();
        }
    }
    

    When you run the Application class, you'll see "Hello, World!" printed on the console.

Remember, the @Component annotation by itself won't get your beans picked up by Spring. You need to ensure that component scanning is enabled and configured to scan the appropriate packages.

  1. Using @Component annotation in Spring:

    • The @Component annotation is a generic stereotype annotation indicating that the class is a Spring component.
    // ExampleComponent.java
    import org.springframework.stereotype.Component;
    
    @Component
    public class ExampleComponent {
        // Component logic...
    }
    
  2. Spring @Component example project:

    • Create a simple Spring project showcasing the use of the @Component annotation.
    // ExampleComponent.java
    import org.springframework.stereotype.Component;
    
    @Component
    public class ExampleComponent {
        // Component logic...
    }
    
    // MainApplication.java
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class MainApplication {
        public static void main(String[] args) {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.example");
            ExampleComponent exampleComponent = context.getBean(ExampleComponent.class);
            // Use exampleComponent...
            context.close();
        }
    }
    
  3. Creating Spring components with @Component annotation:

    • Use the @Component annotation to create Spring components.
    // MyComponent.java
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyComponent {
        // Component logic...
    }
    
  4. How to scan and register components in Spring using @Component:

    • Components annotated with @Component are automatically registered by Spring component scanning.
    // AppConfig.java
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan("com.example.components")
    public class AppConfig {
        // Configuration details...
    }
    
  5. Dependency injection with @Component in Spring:

    • Perform dependency injection using the @Autowired annotation with @Component-annotated classes.
    // ClientComponent.java
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ClientComponent {
    
        private final ExampleComponent exampleComponent;
    
        @Autowired
        public ClientComponent(ExampleComponent exampleComponent) {
            this.exampleComponent = exampleComponent;
        }
    
        // Use exampleComponent...
    }
    
  6. Customizing component names with @Component in Spring:

    • Customize the name of the component using the value attribute of the @Component annotation.
    // MyComponent.java
    import org.springframework.stereotype.Component;
    
    @Component("customName")
    public class MyComponent {
        // Component logic...
    }
    
  7. Working with @ComponentScan and @Component in Spring:

    • Use @ComponentScan to enable component scanning and discover @Component-annotated classes.
    // AppConfig.java
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan("com.example.components")
    public class AppConfig {
        // Configuration details...
    }
    
  8. Spring @Component vs @Service vs @Repository annotation:

    • While @Component, @Service, and @Repository are all specialized forms of @Component, they carry additional semantic meaning.
    // ExampleService.java
    import org.springframework.stereotype.Service;
    
    @Service
    public class ExampleService {
        // Service logic...
    }
    
    // ExampleRepository.java
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class ExampleRepository {
        // Repository logic...
    }