📘 Premium Read: Access my best content on
Medium member-only articles
— deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount):
My Udemy Courses - Ramesh Fadatare
— All my Udemy courses are real-time and project oriented courses.
Spring Stereotype annotations are used to create Spring beans automatically in the application context (Spring IoC container).
The main stereotype annotation is @Component. By using this annotation, Spring provides more Stereotype meta-annotations such as @Service, @Repository, and @Controller.
@Repository is used to create Spring beans for the repositories at the DAO layer.
@Controller is used to create Spring beans at the controller layer.
YouTube Video - @Controller, @Repository and @Service
@Component
The @Component annotation indicates that an annotated class is a “spring bean/component”. Such classes are considered candidates for auto-detection when using annotation-based configuration and classpath scanning.
Here's an example of a simple Java class that is annotated as a Spring component:
import org.springframework.stereotype.Component;
@Component
public class HelloWorldComponent {
public void printHello() {
System.out.println("Hello, Spring!");
}
}
In this example, the @Component annotation tells Spring that this class should be treated as a bean.
Next, you can use this bean in other parts of your application by injecting it using the @Autowired annotation, as in:
import org.springframework.beans.factory.annotation.Autowired;
@Component
public class AnotherBean {
@Autowired
private HelloWorldComponent helloWorldComponent;
public void printHello() {
helloWorldComponent.printHello();
}
}
Next, you need to specify the package to scan for classes annotated with @Component using the @ComponentScan annotation in a Spring configuration class. This will automatically register any classes annotated with @Component as beans in the application context.
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.javaguides.beans")
public class AppConfig {
}
The above configuration will scan the package "com.example.javaguides.beans" for classes annotated with @Component, @Service, @Repository, and @Controller and register them as beans in the Spring application context.
@Service
The @Service annotation is also a specialization of @Component annotation. Basically, @Service annotation is used to create Spring beans at the Service layer.
import org.springframework.stereotype.Service;
@ServicepublicclassUserServiceImplimplementsUserService{
// All the service layer logic goes here
}
The @Repository annotation is also a specialization of @Component annotation. Basically, @Repository is used to create Spring beans for the repositories at the DAO layer.
import org.springframework.stereotype.Repository;
@RepositorypublicclassUserDaoImplimplementsUserDao{
// All the dao layer logic goes here
}
The @Controller annotation is also a specialization of @Component annotation. Basically, @Controller is used to create Spring beans at the controller layer.
Comments
Post a Comment
Leave Comment