Skip to main content

annotation

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

Comments

Popular posts from this blog

Mockito interview Questions

1.       Question 1. What Is Mockito? Answer : Mockito allows creation of mock object for the purpose of Test Driven Development and Behavior Driven development. Unlike creating actual object, Mockito allows creation of fake object (external dependencies) which allows it to give consistent results to a given invocation. 2.       Question 2. Why Do We Need Mockito? What Are The Advantages? Answer : Mockito differentiates itself from the other testing framework by removing the expectation beforehand. So, by doing this, it reduces the coupling. Most of the testing framework works on the "expect-run-verify". Mockito allows it to make it "run-verify" framework. Mockito also provides annotation which allows to reduce the boilerplate code. 3.       Question 3. Can You Explain A Mockito Framework? Answer : In Mockito, you always check a particular class. The dependency in that class is injected using m...

REST Methods

GET ============================================= HTTP GET method is used to **read** (or retrieve) a representation of a resource. According to the design of the HTTP specification, GET requests are used only to read data and not change it. Therefore, when used this way, they are considered safe. That is, they can be called without risk of data modification or corruption. Means calling it once has the same effect as calling it 10 times. Additionally, GET is idempotent which means that making multiple identical requests ends up having the same result as a single request. Donot expose unsafe operations via GET. It should never modify any resources on the server. Example: ------------    GET http://www.example.com/customers/12345/orders POST =================================== -> The POST verb is most-often utilized to **create** new resources. In particular, it's used to create subordinate resources. -> That is, subordinate to some other (e.g. parent) reso...