filmov
tv
Annotations in Spring Boot Interview Questions and Answers | Code Decode | Part -2

Показать описание
In this video of code decode of Annotations in Spring boot Interview Questions and Answers we have covered some common interview questions regarding spring boot annotations
Udemy Course of Code Decode on Microservice k8s AWS CICD link:
Course Description Video :
It’s the way to implement Dependency Injection in spring / spring boot application.
Container will provide the required bean to dependent bean at runtime
Spring provides annotation-based auto-wiring by providing @Autowired annotation.
It is used to autowire spring bean on setter methods, instance variable, and constructor.
When we use @Autowired annotation, the spring container auto-wires the bean by matching data-type.
How will container autowire the required bean in dependent bean at runtime ? It first need to create beans during startup so that it can provide beans at later stage.
How will container know who POJO / java class should be considered as bean and whom to ignore ?
@Component: It is a class-level annotation.
It is used to mark a Java class as a bean.
A Java class annotated with @Component is found during the classpath.
The Spring Framework pick it up and configure it in the application context as a Spring Bean.
It is a generic stereotype for any Spring-managed component. The specializations are @Controller, @Service, @Repository
The @Controller is a class-level annotation.
It is a specialization of @Component.
It marks a class as a web request handler. It is often used to serve web pages. It is used in conjunction with @RequestMapping annotation.
We also use @RestController when we need to send response in JSON format directly.
By default, @Controller returns a string that indicates which route to redirect. Its used mostly with JSPs. It returns URL to new JSP page where it has to be redirected
They are nothing but the specialized form of @Component annotation for certain situations. Instead of using @Component on a controller class in Spring MVC, we use @Controller, which is more readable and appropriate.
The @Controller is a class-level annotation.
It is a specialization of @Component.
It marks a class as a web request handler. It is often used to serve web pages. It is used in conjunction with @RequestMapping annotation.
We also use @RestController when we need to send response in JSON format directly.
They are nothing but the specialized form of @Component annotation for certain situations. Instead of using @Component on a controller class in Spring MVC, we use @Controller, which is more readable and appropriate.
By using @Controller annotation we do two things,
We declare that this class is a Spring bean and should be created and maintained by Spring ApplicationContext,
We indicate that its a controller in MVC setup. This latter property is used by web-specific tools and functionalities.
DispatcherServlet will look for @RequestMapping on classes that are annotated using @Controller but not with @Component.
This means @Component and @Controller are the same with respect to bean creation and dependency injection but @Controller is a specialized form of @Component. Even if you replace @Controller annotation with @Compoenent, Spring can automatically detect and register the controller class but it may not work as you expect with respect to request mapping.
It is also used at class level.
It is a specialization of @Component.
It tells the Spring that class contains the business logic.
But at the end The main task of this annotation is also to mark the class capable to become a bean in Spring container similar to component. We can use @component also and it will work the same as this annotation does.
Subscriber and Follow Code Decode
#annotations #codedecode #springbootinterviewquestions
Udemy Course of Code Decode on Microservice k8s AWS CICD link:
Course Description Video :
It’s the way to implement Dependency Injection in spring / spring boot application.
Container will provide the required bean to dependent bean at runtime
Spring provides annotation-based auto-wiring by providing @Autowired annotation.
It is used to autowire spring bean on setter methods, instance variable, and constructor.
When we use @Autowired annotation, the spring container auto-wires the bean by matching data-type.
How will container autowire the required bean in dependent bean at runtime ? It first need to create beans during startup so that it can provide beans at later stage.
How will container know who POJO / java class should be considered as bean and whom to ignore ?
@Component: It is a class-level annotation.
It is used to mark a Java class as a bean.
A Java class annotated with @Component is found during the classpath.
The Spring Framework pick it up and configure it in the application context as a Spring Bean.
It is a generic stereotype for any Spring-managed component. The specializations are @Controller, @Service, @Repository
The @Controller is a class-level annotation.
It is a specialization of @Component.
It marks a class as a web request handler. It is often used to serve web pages. It is used in conjunction with @RequestMapping annotation.
We also use @RestController when we need to send response in JSON format directly.
By default, @Controller returns a string that indicates which route to redirect. Its used mostly with JSPs. It returns URL to new JSP page where it has to be redirected
They are nothing but the specialized form of @Component annotation for certain situations. Instead of using @Component on a controller class in Spring MVC, we use @Controller, which is more readable and appropriate.
The @Controller is a class-level annotation.
It is a specialization of @Component.
It marks a class as a web request handler. It is often used to serve web pages. It is used in conjunction with @RequestMapping annotation.
We also use @RestController when we need to send response in JSON format directly.
They are nothing but the specialized form of @Component annotation for certain situations. Instead of using @Component on a controller class in Spring MVC, we use @Controller, which is more readable and appropriate.
By using @Controller annotation we do two things,
We declare that this class is a Spring bean and should be created and maintained by Spring ApplicationContext,
We indicate that its a controller in MVC setup. This latter property is used by web-specific tools and functionalities.
DispatcherServlet will look for @RequestMapping on classes that are annotated using @Controller but not with @Component.
This means @Component and @Controller are the same with respect to bean creation and dependency injection but @Controller is a specialized form of @Component. Even if you replace @Controller annotation with @Compoenent, Spring can automatically detect and register the controller class but it may not work as you expect with respect to request mapping.
It is also used at class level.
It is a specialization of @Component.
It tells the Spring that class contains the business logic.
But at the end The main task of this annotation is also to mark the class capable to become a bean in Spring container similar to component. We can use @component also and it will work the same as this annotation does.
Subscriber and Follow Code Decode
#annotations #codedecode #springbootinterviewquestions
Комментарии