1 @required
Ans:===================
Case1 =>Spring 2.0 has introduced a annotation @required and dependency check has been removed from the 2.0. Using @required annotation you can make a priticular propertey has been set with value or not.
case 2=>since setter injection is optional if we want to make that is mandatory the we have to go for the @required annotation.for that you have the write the @required at setter level.
public class Motor {
private int motorId;
private String motorName;
@Required //this will make to setter as mandatory
public void setMotorId(int motorId) {
this.motorId = motorId;
}
public void setMotorName(String motorName) {
this.motorName = motorName;
}
================================================================================================================
2 @Qualifier
Ans:-
In Spring, @Qualifier means, which bean is qualify to autowired on a field.
Lets try i have a class called motor. which has one property called engine which i want to inject, then its ok. but suppose i have two bean with the type of engine -->one is simple engine and second one is maruti engine .so i want to inject both as a bean in the motor class by using @ autowired. so it will going to
====================================================================================================================
3 @autowired
Ans:--
Spring @Autowired annotation is used for automatic dependency injection. Spring framework is built on dependency injection and we inject the class dependencies through spring bean configuration file.
public class Robot {
//1 attribute level
@Autowired
private Chip chip;
//private String robotName;
/*//2 setter level
//@Autowired
//@Required
public void setChip(Chip chip) {
this.chip = chip;
}*/
@Override
public String toString() {
return "Robot [chip=" + chip + "]";
}
public Robot() {
System.out.println("Robot is created:!!!");
}
}
================================================================================================================
4 What’s the difference between @Component, @Controller, @Repository & @Service annotations in Spring?
Ans:---
@Component annotation marks a java class as a bean so the component-scanning mechanism of spring can pick it up and pull it into the application context. To use this annotation, apply it over class as below:
ex:
@Component
public class EmployeeDAOImpl implements EmployeeDAO {
...
}
@Service annotation is used in your service layer and annotates classes that perform service tasks, often you don't use it but in many case you use this annotation to represent a best practice. For example, you could directly call a DAO class to persist an object to your database but this is horrible. It is pretty good to call a service class that calls a DAO. This is a good thing to perform the separation of concerns pattern.
@Controller annotation is an annotation used in Spring MVC framework (the component of Spring Framework used to implement Web Application). The @Controller annotation indicates that a particular class serves the role of a controller. The @Controller annotation acts as a stereotype for the annotated class, indicating its role. The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations.
So looking at the Spring MVC architecture you have a DispatcherServlet class (that you declare in your XML configuration) that represent a front controller that dispatch all the HTTP Request towards the appropriate controller classes (annotated by @Controller). This class perform the business logic (and can call the services) by its method. These classes (or its methods) are typically annotated also with @RequestMapping annotation that specify what HTTP Request is handled by the controller and by its method.
For example:
@Controller
@RequestMapping("/appointments")
public class AppointmentsController {
private final AppointmentBook appointmentBook;
@Autowired
public AppointmentsController(AppointmentBook appointmentBook) {
this.appointmentBook = appointmentBook;
}
@RequestMapping(method = RequestMethod.GET)
public Map<String, Appointment> get() {
return appointmentBook.getAppointmentsForToday();
}
This class is a controller.
This class handles all the HTTP Request toward "/appointments" "folder" and in particular the get method is the method called to handle all the GET HTTP Request toward the folder "/appointments".
===========================================================================================================================
MVC--
Describe DispatcherServlet.
Explain WebApplicationContext
@Controller annotation
@RequestMapping annotation
Core--
@Bean
@Qualifier
@Autowired
@Required
@Configuration
@Controller
@Service
@Repository
@Inject
@Resource
@Component
@RequestMapping
@Transactional
@ModelAttribute
Ans:===================
Case1 =>Spring 2.0 has introduced a annotation @required and dependency check has been removed from the 2.0. Using @required annotation you can make a priticular propertey has been set with value or not.
case 2=>since setter injection is optional if we want to make that is mandatory the we have to go for the @required annotation.for that you have the write the @required at setter level.
public class Motor {
private int motorId;
private String motorName;
@Required //this will make to setter as mandatory
public void setMotorId(int motorId) {
this.motorId = motorId;
}
public void setMotorName(String motorName) {
this.motorName = motorName;
}
================================================================================================================
2 @Qualifier
Ans:-
In Spring, @Qualifier means, which bean is qualify to autowired on a field.
Lets try i have a class called motor. which has one property called engine which i want to inject, then its ok. but suppose i have two bean with the type of engine -->one is simple engine and second one is maruti engine .so i want to inject both as a bean in the motor class by using @ autowired. so it will going to
====================================================================================================================
3 @autowired
Ans:--
Spring @Autowired annotation is used for automatic dependency injection. Spring framework is built on dependency injection and we inject the class dependencies through spring bean configuration file.
public class Robot {
//1 attribute level
@Autowired
private Chip chip;
//private String robotName;
/*//2 setter level
//@Autowired
//@Required
public void setChip(Chip chip) {
this.chip = chip;
}*/
@Override
public String toString() {
return "Robot [chip=" + chip + "]";
}
public Robot() {
System.out.println("Robot is created:!!!");
}
}
================================================================================================================
4 What’s the difference between @Component, @Controller, @Repository & @Service annotations in Spring?
Ans:---
@Component annotation marks a java class as a bean so the component-scanning mechanism of spring can pick it up and pull it into the application context. To use this annotation, apply it over class as below:
ex:
@Component
public class EmployeeDAOImpl implements EmployeeDAO {
...
}
@Service annotation is used in your service layer and annotates classes that perform service tasks, often you don't use it but in many case you use this annotation to represent a best practice. For example, you could directly call a DAO class to persist an object to your database but this is horrible. It is pretty good to call a service class that calls a DAO. This is a good thing to perform the separation of concerns pattern.
@Controller annotation is an annotation used in Spring MVC framework (the component of Spring Framework used to implement Web Application). The @Controller annotation indicates that a particular class serves the role of a controller. The @Controller annotation acts as a stereotype for the annotated class, indicating its role. The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations.
So looking at the Spring MVC architecture you have a DispatcherServlet class (that you declare in your XML configuration) that represent a front controller that dispatch all the HTTP Request towards the appropriate controller classes (annotated by @Controller). This class perform the business logic (and can call the services) by its method. These classes (or its methods) are typically annotated also with @RequestMapping annotation that specify what HTTP Request is handled by the controller and by its method.
For example:
@Controller
@RequestMapping("/appointments")
public class AppointmentsController {
private final AppointmentBook appointmentBook;
@Autowired
public AppointmentsController(AppointmentBook appointmentBook) {
this.appointmentBook = appointmentBook;
}
@RequestMapping(method = RequestMethod.GET)
public Map<String, Appointment> get() {
return appointmentBook.getAppointmentsForToday();
}
This class is a controller.
This class handles all the HTTP Request toward "/appointments" "folder" and in particular the get method is the method called to handle all the GET HTTP Request toward the folder "/appointments".
===========================================================================================================================
MVC--
Describe DispatcherServlet.
Explain WebApplicationContext
@Controller annotation
@RequestMapping annotation
Core--
@Bean
@Qualifier
@Autowired
@Required
@Configuration
@Controller
@Service
@Repository
@Inject
@Resource
@Component
@RequestMapping
@Transactional
@ModelAttribute
Comments
Post a Comment