@Component is a generic stereotype for Spring-managed components.
@Service is a specialization of @Component for service classes.
@Bean is used in Java configuration classes to define and configure individual bean instances explicitly.

 

 

@Component:
@Component is used to mark a class as a generic Spring-managed component. Here's an example:
import org.springframework.stereotype.Component;

@Component
public class MyComponent {
    // This class is a Spring-managed component.
}
In this example, MyComponent is marked as a Spring component using @Component. It will be automatically detected and registered in the Spring application context. 


@Service:
@Service is a specialization of @Component and is typically used for service classes. Here's an example:
import org.springframework.stereotype.Service;

@Service
public class MyService {
    // This class is a Spring service.
}
In this example, MyService is marked as a Spring service using @Service. It's a more specific annotation than @Component, indicating that this class is a service component.

@Bean:
@Bean is used in Java configuration classes to explicitly define and configure individual bean instances. Here's an example:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    
    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

public class MyBean {
    // This is a custom Spring bean.
}
In this example, we have a Java configuration class AppConfig. The @Bean annotation is used on a method to define a custom bean myBean. This method is responsible for creating and configuring the MyBean instance explicitly.

These examples demonstrate how to use @Component, @Service, and @Bean to define Spring beans in different contexts. @Component and @Service are typically used for class-level annotations, while @Bean is used at the method level within a configuration class to define and configure individual beans.