• +91 9723535972
  • info@interviewmaterial.com

Spring Interview Questions and Answers

Spring Interview Questions and Answers

Question - 51 : - What do you understand by auto wiring and name the different modes of it?

Answer - 51 : -

The Spring container is able to autowire relationships between the collaborating beans. That is, it is possible to let Spring resolve collaborators for your bean automatically by inspecting the contents of the BeanFactory.
Different modes of bean auto-wiring are:

  • no: This is default setting which means no autowiring. Explicit bean reference should be used for wiring.
  • byName: It injects the object dependency according to name of the bean. It matches and wires its properties with the beans defined by the same names in the XML file.
  • byType: It injects the object dependency according to type. It matches and wires a property if its type matches with exactly one of the beans name in XML file.
  • constructor: It injects the dependency by calling the constructor of the class. It has a large number of parameters.
  • autodetect: First the container tries to wire using autowire by constructor, if it can’t then it tries to autowire by byType.

Question - 52 : - What are the limitations with auto wiring?

Answer - 52 : -

Following are some of the limitations you might face with auto wiring:

  • Overriding possibility: You can always specify dependencies using and settings which will override autowiring.
  •  Primitive data type: Simple properties such as primitives, Strings and Classes can’t be autowired.
  • Confusing nature: Always prefer using explicit wiring because autowiring is less precise.

Question - 53 : - What do you mean by  Annotation-based container configuration?

Answer - 53 : -

Instead of using XML to describe a bean wiring, the developer moves the configuration into the component class itself by using annotations on the relevant class, method, or field declaration. It acts as an alternative to XML setups. For example:

  1. @Configuration
  2. public class AnnotationConfig
  3. {
  4. @Bean
  5. public MyDemo myDemo()
  6.  { return new MyDemoImpll(); }
  7. }

Question - 54 : - What do you understand by @Required annotation?

Answer - 54 : -

@Required is applied to bean property setter methods. This annotation simply indicates that the affected bean property must be populated at the configuration time with the help of an explicit property value in a bean definition or with autowiring. If the affected bean property has not been populated, the container will throw BeanInitializationException.

For example:

  • public class Employee
  • {
  • private String name;
  • @Required
  • public void setName(String name)
  • {this.name=name; }
  • public string getName()
  • { return name; }
  • }

Question - 55 : -
What do you understand by @Autowired annotation?

Answer - 55 : -

The @Autowired annotation provides more accurate control over where and how autowiring should be done. This annotation is used to autowire bean on the setter methods, constructor, a property or methods with arbitrary names or multiple arguments. By default, it is a type driven injection.

For Example:

  1. public class Employee
  2. {
  3. private String name;
  4. @Autowired
  5. public void setName(String name)
  6. {this.name=name; }
  7. public string getName()
  8. { return name; }
  9. }

Question - 56 : - What do you understand by @Qualifier annotation?

Answer - 56 : -

When you create more than one bean of the same type and want to wire only one of them with a property  you can use the @Qualifier annotation along with @Autowired to remove the ambiguity by specifying which exact bean should be wired.

For example, here we have two classes, Employee and EmpAccount respectively. In EmpAccount, using @Qualifier its specified that bean with id emp1 must be wired.

Employee.java

  1. public class Employee
  2. {
  3. private String name;
  4. @Autowired
  5. public void setName(String name)
  6. { this.name=name; }
  7. public string getName()
  8. { return name; }
  9. }
EmpAccount.java

  1. public class EmpAccount
  2. {
  3. private Employee emp;
  4. @Autowired
  5. @Qualifier(emp1)
  6. public void showName()
  7. {
  8. System.out.println(“Employee name : ”+emp.getName);
  9. }
  10. }

Question - 57 : - What do you understand by @RequestMapping annotation?

Answer - 57 : -

@RequestMapping annotation is used for mapping a particular HTTP request method to a specific class/ method in controller that will be handling the respective request. This annotation can be applied at both levels:

  • Class level : Maps the URL of the request
  • Method level: Maps the URL as well as HTTP request method

Question - 58 : - Explain WebApplicationContext.

Answer - 58 : -

The WebApplicationContext is an extension of the plain ApplicationContext. It has some extra features that are necessary for web applications. It differs from a normal ApplicationContext in terms of its capability of resolving themes and in deciding which servlet it is associated with.

Question - 59 : - Describe DispatcherServlet.

Answer - 59 : -

The DispatcherServlet is the core of Spring Web MVC framework. It handles all the HTTP requests and responses. The DispatcherServlet receives the entry of handler mapping from the configuration file and forwards the request to the controller. The controller then returns an object of Model And View. The DispatcherServlet checks the entry of view resolver in the configuration file and calls the specified view component.

Question - 60 : - What do you mean by Spring MVC framework?

Answer - 60 : - The Spring web MVC framework provides model-view-controller architecture and ready to use components that are used to develop flexible and loosely coupled web applications. The MVC pattern helps in separating the different aspects of the application like input logic, business logic and UI logic, while providing a loose coupling between all these elements.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners