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...

JAVA Expert Interview Questions Answers 2017

Java Basics ::  Interview Questions and Answers Home  »  Interview Questions  »  Technical Interview  »  Java Basics  » Interview Questions 1.     What is the difference between a constructor and a method? A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 2.     What is the purpose of garbage collection in Java, and when is it used? The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. 3.  ...

Java Example Program to Convert List to Set

import java.util.ArrayList; import java.util.HashSet; import java.util.Set; public class ListToSet {  /**   * @author Amarjit Kumar   * @category interview questions   *   * Description: Convert List to set in java with example program   *   */  public static void main(String[] args) {   ArrayList<String> arrList= new ArrayList<>();   arrList.add("Java");   arrList.add("Java");   arrList.add("List to String");   arrList.add("Example Program");   Set<String> strSet = new HashSet<String>(arrList);   System.out.println(strSet);   System.out.println(arrList);  } } /*  * Java program to convert list to set. Convert ArrayList of string to HashSet  * in java example program How to convert List to Set in java Set<String> strSet  * = new HashSet<String>(arrList); HashSet having a constructor which will take  * list as an ar...