Difference between Component Service Repository Controller & RestController annotation | Code Decode

Difference between Component Service Repository Controller & RestController annotation | Code Decode

21.683 Lượt nghe
Difference between Component Service Repository Controller & RestController annotation | Code Decode
Spring boot Interview Question @Service @Repository @Component @Controller and @RestController differences Udemy Course of Code Decode on Microservice k8s AWS CICD link: https://openinapp.co/udemycourse Course Description Video : https://yt.openinapp.co/dmjvd What is spring annotations 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. @Component, @Service, @Repository, @Controller all these are stereotype annotations What are stereotype annotation ? A "stereotype annotation" is a special label or tag that developers put on classes to tell Spring something about the role or purpose of that class. These annotations help the program understand how to treat and use these classes. They're like quick notes attached to classes, providing information on what each class is meant to do in the application. How Spring treats these stereotype annotations Spring treats stereotype annotations differently from other annotations primarily in the context of component scanning and bean registration. Here's how Spring treats stereotype annotations differently: Automatic Bean Registration: Classes annotated with stereotype annotations (e.g., @Component, @Service, @Repository, @Controller) are automatically registered as Spring beans during the component scanning process. Other non-stereotype annotations or custom annotations generally don't trigger automatic bean registration. How Spring treats these stereotype annotations Component Scanning: Stereotype annotations are explicitly considered during component scanning. Spring looks for classes with these annotations and treats them differently based on their roles. Other annotations might be used for specific purposes, but they might not trigger the same automatic bean registration and special treatment. How Spring treats these stereotype annotations Convention-Over-Configuration: Stereotype annotations align with the convention-over-configuration principle in Spring. By using these annotations, developers adhere to a set of conventions that Spring can understand without requiring extensive configuration. Other annotations might still require manual configuration to achieve similar results. In summary, Spring treats stereotype annotations differently by providing them with special roles in the framework. These annotations are designed to make development more straightforward by relying on conventions and minimizing the need for explicit configuration. They play a key role in the automatic discovery and management of beans within the Spring container. How Spring treats these stereotype annotations Example of non stereotype annotations : @RequestMapping is not a stereotype annotation but is used for handling the mapping of requests. @Autowired is not a stereotype annotation but rather an annotation for dependency injection. Thus When Spring sees these annotations, it doesnt create beans automatically in container @Component We can use @Component annotation in our application to declare any class as the Spring’s managed components. The classes under the @ComponentScan path annotated with @Component annotation will be registered as a Bean in Spring ApplicationContext. You can use the instance of such bean using the @Autowired annotations. Most Asked Core Java Interview Questions and Answers: https://youtube.com/playlist?list=PLyHJZXNdCXscoyL5XEZoHHZ86_6h3GWE1 Advance Java Interview Questions and Answers: https://youtube.com/playlist?list=PLyHJZXNdCXsexOO1VQ4vs-BM2-8CKrixd Java 8 Interview Questions and Answers: https://youtube.com/playlist?list=PLyHJZXNdCXsdeusn4OM33415DCMQ6sUKy Hibernate Interview Questions and Answers: https://youtube.com/playlist?list=PLyHJZXNdCXsdC-p2186C6NO4FpadnCC_q Spring Boot Interview Questions and Answers: https://youtube.com/playlist?list=PLyHJZXNdCXsexOO1VQ4vs-BM2-8CKrixd Angular Playlist: https://www.youtube.com/watch?v=CAl7RQSdq2Q&list=PLyHJZXNdCXsfxRtDwtGkDD_lLfTWc1g0i SQL Playlist: https://www.youtube.com/playlist?list=PLyHJZXNdCXse86eLuwy5uZohd_bddE9Ni GIT: https://youtube.com/playlist?list=PLyHJZXNdCXscpl6pxOnL2lRWJlzvzjyZE Subscriber and Follow Code Decode Subscriber Code Decode: https://www.youtube.com/c/CodeDecode?sub_confirmation=1 LinkedIn : https://www.linkedin.com/in/codedecodeyoutube/ Instagram: https://www.instagram.com/codedecode25/ #pathvariable #codedecode #requestparam