• +91 9723535972
  • info@interviewmaterial.com

Spring Interview Questions and Answers

Spring Interview Questions and Answers

Question - 41 : - In how many ways can Dependency Injection be done?

Answer - 41 : -

In general, dependency injection can be done in three ways, namely :

  • Constructor Injection
  • Setter Injection
  • Interface Injection

Question - 42 : - How many types of IOC containers are there in spring?

Answer - 42 : -

  • BeanFactory: BeanFactory is like a factory class that contains a collection of beans. It instantiates the bean whenever asked for by clients.
  • ApplicationContext: The ApplicationContext interface is built on top of the BeanFactory interface. It provides some extra functionality on top BeanFactory.

Question - 43 : - Differentiate between constructor injection and setter injection.

Answer - 43 : -

Constructor Injection vs Setter Injection

Constructor Injection

Setter Injection

There is no partial injection.

There can be partial injection.

It doesn’t override the setter property.

It overrides the constructor property.

It will create a new instance if any modification is done.

It will not create new instance if any modification is done.

It works better for many properties.

It works better for few properties.

Question - 44 : - List some of the benefits of IoC.

Answer - 44 : -

Some of the benefits of IoC are:

  • It will minimize the amount of code in your application.
  • It will make your application easy to test because it doesn’t require any singletons or JNDI lookup mechanisms in your unit test cases.
  • It promotes loose coupling with minimal effort and least intrusive mechanism.
  • It supports eager instantiation and lazy loading of the services.

Question - 45 : - Explain Spring Beans?

Answer - 45 : -

  • They are the objects that form the backbone of the user’s application.
  • Beans are managed by the Spring IoC container.
  • They are instantiated, configured, wired and managed by a Spring IoC container
  • Beans are created with the configuration metadata that the users supply to the container.

Question - 46 : - How configuration metadata is provided to the Spring container?

Answer - 46 : -

Configuration metadata can be provided to Spring container in following ways:

XML-Based configuration: In Spring Framework, the dependencies and the services needed by beans are specified in configuration files which are in XML format. These configuration files usually contain a lot of bean definitions and application specific configuration options. They generally start with a bean tag. For example:
  •  
Annotation-Based configuration: Instead of using XML to describe a bean wiring, you can configure the bean into the component class itself by using annotations on the relevant class, method, or field declaration. By default, annotation wiring is not turned on in the Spring container. So, you need to enable it in your Spring configuration file before using it. For example:
Java-based configuration: The key features in Spring Framework’s new Java-configuration support are @Configuration annotated classes and @Bean annotated methods. 
1. @Bean annotation plays the same role as the element. 

2.@Configuration classes allows to define inter-bean dependencies by simply calling other @Bean methods in the same class. 

For example:

  • @Configuration
  • public class StudentConfig 
  • @Bean
  • public StudentBean myStudent() 
  • { return new StudentBean(); }
  • }

Question - 47 : - How many bean scopes are supported by Spring?

Answer - 47 : -

The Spring Framework supports five scopes. They are:

  • Singleton: This provides scope for the bean definition to single instance per Spring IoC container.
  • Prototype: This provides scope for a single bean definition to have any number of object instances.
  • Request: This provides scope for a bean definition to an HTTP-request. 
  • Session: This provides scope for a bean definition to an HTTP-session. 
  • Global-session: This provides scope for a bean definition to an Global HTTP-session. 

Question - 48 : - What is the Bean life cycle in Spring Bean Factory Container?

Answer - 48 : -

Bean life cycle in Spring Bean Factory Container is as follows:

  • The Spring container instantiates the bean from the bean’s definition in the XML file.
  • Spring populates all of the properties using the dependency injection, as specified in the bean definition.
  • The factory calls setBeanName() by passing the bean’s ID, if the bean implements the BeanNameAware interface.
  • The factory calls setBeanFactory() by passing an instance of itself, if the bean implements the BeanFactoryAware interface.
  • preProcessBeforeInitialization() methods are called if there are any BeanPostProcessors associated with the bean.
  • If an init-method is specified for the bean, then it will be called.
  • Finally, postProcessAfterInitialization() methods will be called if there are any BeanPostProcessors associated with the bean.

Question - 49 : - Explain inner beans in Spring.

Answer - 49 : -

A bean can be declared as an inner bean only when it is used as a property of another bean. For defining a bean, the Spring’s XML based configuration metadata provides the use of element inside the or . Inner beans are always anonymous and they are always scoped as prototypes. For example, let’s say we have one Student class having reference of Person class. Here we will be creating only one instance of Person class and use it inside Student.

Here’s a Student class followed by bean configuration file:

Student.java

  • public class Student
  • {
  • private Person person;
  • //Setters and Getters
  • }
  • public class Person
  • {
  • private String name;
  • private String address;
  • //Setters and Getters
  • }
studentbean.xml

Question - 50 : -
Define Bean Wiring.

Answer - 50 : -

When beans are combined together within the Spring container, it’s called wiring or bean wiring. The Spring container needs to know what beans are needed and how the container should use dependency injection to tie the beans together, while wiring beans.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners