• +91 9723535972
  • info@interviewmaterial.com

Spring Interview Questions and Answers

Spring Interview Questions and Answers

Question - 11 : - What Spring Sub-Projects Do You Know? Describe Them Briefly.

Answer - 11 : -

  • Core – a key module that provides fundamental parts of the framework, such as IoC or DI
  • JDBC – enables a JDBC-abstraction layer that removes the need to do JDBC coding for specific vendor databases
  • ORM integration – provides integration layers for popular object-relational mapping APIs, such as JPA, JDO and Hibernate
  • Web – a web-oriented integration module that provides multipart file upload, Servlet listeners and web-oriented application context functionalities
  • MVC framework – a web module implementing the Model View Controller design pattern
  • AOP module – aspect-oriented programming implementation allowing the definition of clean method-interceptors and pointcuts

Question - 12 : - How Can We Inject Beans in Spring?

Answer - 12 : -

A few different options exist in order to inject Spring beans:

  • Setter injection
  • Constructor injection
  • Field injection
The configuration can be done using XML files or annotations.

For more details, check this article.

Question - 13 : - Which Is the Best Way of Injecting Beans and Why?

Answer - 13 : -

The recommended approach is to use constructor arguments for mandatory dependencies and setters for optional ones. This is because constructor injection allows injecting values to immutable fields and makes testing easier.

Question - 14 : - What Is the Difference Between BeanFactory and ApplicationContext?

Answer - 14 : -

BeanFactory is an interface representing a container that provides and manages bean instances. The default implementation instantiates beans lazily when getBean() is called.

In contrast, ApplicationContext is an interface representing a container holding all information, metadata and beans in the application. It also extends the BeanFactory interface, but the default implementation instantiates beans eagerly when the application starts. However, this behavior can be overridden for individual beans.

For all differences, please refer to the documentation.

Question - 15 : - What Is a Spring Bean?

Answer - 15 : -

The Spring Beans are Java Objects that are initialized by the Spring IoC container.

Question - 16 : - What Is the Default Bean Scope in Spring Framework?

Answer - 16 : -

By default, a Spring Bean is initialized as a singleton.

Question - 17 : - How to Define the Scope of a Bean?

Answer - 17 : -

In order to set Spring Bean's scope, we can use @Scope annotation or “scope” attribute in XML configuration files. Note that there are five supported scopes:

  • Singleton
  • Prototype
  • Request
  • Session
  • Global-session

Question - 18 : - Are Singleton Beans Thread-Safe?

Answer - 18 : -

No, singleton beans are not thread-safe, as thread safety is about execution, whereas the singleton is a design pattern focusing on creation. Thread safety depends only on the bean implementation itself.

Question - 19 : - What Does the Spring Bean Life Cycle Look Like?

Answer - 19 : -

First, a Spring bean needs to be instantiated based on Java or XML bean definition. It may also be required to perform some initialization to get it into a usable state. After that, when the bean is no longer required, it will be removed from the IoC container.

Question - 20 : - What Is the Spring Java-Based Configuration?

Answer - 20 : -

It's one of the ways of configuring Spring-based applications in a type-safe manner. It's an alternative to the XML-based configuration.

Also, to migrate a project from XML to Java config,


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners