Top 50 Spring Mvc Framework Interview Questions You Must Prepare 19.Mar.2024

The various custom editors provided by the Spring Framework are:

  • PropertyEditor
  • URLEditor
  • ClassEditor
  • CustomDateEditor
  • FileEditor
  • LocaleEditor
  • StringArrayPropertyEditor
  • StringTrimmerEditor

Autowiring has five different modes:

  • no: no autowire
  • byName : Autowiring that can be done by property name
  • byType : property type as autowired
  • constructor: It is similar to byType and it is property is in constructor
  • autodetect :  Spring is allowed to select autowiring from byType or constructor

The different types of loC are: –

  • Setter Injection: With the help of JavaBeans properties.
  • Constructor Injection: Dependencies are given in the form of constructor parameters.
  • Interface Injection: With the help of an interface, an Injection is performed.

Out of these three, only construction and setter are being used in Spring.

They are:

  • ClassPathXmlApplicationContext
  • FileSystemXmlApplicationContext
  • XmlWebApplicationContext

DataAccessException is a RuntimeException. It is an Unchecked Exception. The user cannot be forced to handle these kinds of exceptions.

Two types of dependency injection are supported by spring framework:

  • Setter Injection
  • Constructor Injection

There are two ways to access hibernate using spring:

  • Inversion of Control with a Hibernate Template and Callback
  • Extending HibernateDAOSupport and Applying an AOP Interceptor node.

The database code can be kept clean and simple by using the DAO module. This helps in preventing problems that rise because of poor handling of closures of database resources. Also, the DAO module utilizes the AOP module to enable objects in the Spring application to use transaction management services.

When only a small amount of transactional operations is there, it is advised to use Programmatic transaction management. But if there is a big amount of transactional operations to be taken care of, declarative transaction management is preferred.

There are three types of Injection in spring:

  • Setter Injection
  • Constructor Injection
  • Getter or Method Injection

Bean factory interface is used to provide configuration framework for object creation and basic functionality around object management.

Spring is set to be a framework which helps Java programmer for development of code and it provides IOC container, Dependency Injector, MVC flow and many other APIs for the java programmer.

Singleton Bean: – Single bean definition to a single object instance per Spring IOC container

Prototype Bean: – Single bean definition to any number of object instances per Spring IOC Container

AOP module is utilized for creating aspects for Spring applications. It also enables support for metadata programming in Spring.

Following are the advantages of spring framework:

  • Layered Architecture
  • Enables Plain Old Java Object (POJO) Programming and it enables continuous integration and testability
  • Dependency Injection and Inversion of Control that simplifies JDBC
  • Open source framework which can be used for commercial purpose

Weaving is used to create new proxy object by applying aspects to target object.

A target is the class that is advised. This class can either be a class to which we want to add a special behavior to or a third party class. The target class is free to center on its major concern using the AOP concepts, regardless of any advice that is being applied.

Transaction management supported by Spring are :

  • Declarative transaction management.
  • Programmatic transaction management.

A Spring MVC is a single shared controller instance and it is used to handle request type controllers, interceptors which run in the IoC container. It also allows multiple Dispatcher Servlets which can share application context interface but not class based interface.

DAO is used to provide integration of Java database connectivity and Object relational mapping objects. DAO is spring framework provides connection for JDBC, hibernate, JDO, JPA, Common client interface and Oracle.

The point where an aspect can be introduced in the application is known as a joinpoint. This point could be a field being modified, a method being called or even an exception being thrown. At these points, the new aspect’s code can be added to introduce a new behavior to the application.

Aspect code can be inserted at this point into normal flow of application to change the current behavior.

Following steps needs to be done to start with the Spring:

  • Download Spring and its dependent file from spring’s site.
  • Create application context xml to define beans and its dependencies
  • Integrate application context xml with web.xml
  • Deploy and Run the application

Declarative transaction management has minimum impact on the application code and, therefore, is an idealistic lightweight container.

Singleton means only one bean is defined per object instance while Prototype means one definition to more than one object instances in Spring.

There are two important methods of Bean life cycle:

  • Setup – called when bean is loaded into container
  • Teardown – called when bean is unloaded into container

Types of advice are:

  • Before advice: Advice that is executed prior to a joinpoint is called the ‘before advice’.
  • After returning advice: Advice that is executed after the normal completion of a joinpoint is called the ‘after returning advice’.
  • After throwing advice: Advice that is executed only if a method exits abnormally by throwing an exception, is called the ‘after throwing advice’.
  • After (finally) advice: Advice that is executed irrespective of how a joinpoint exits is called ‘after finally advice’.
  • Around advice: Advice that borders a joinpoint, for example, a method invocation, is called an ‘around advice’. This can be used to perform special activities before and after the invocation of method.

Advice will tell application on new behavior and it is the implementation of an aspect. It is inserted into an application at the joinpoint.

Advice is the implementation of an aspect. It is something like telling your application of a new behavior. Generally, the advice is inserted into an application at joinpoints.

Hibernate can be accessed in the following two ways:

  • By IOC with a Callback and HibernateTemplate.
  • By applying an AOP Interceptor and broadening the HibernateDaoSupport.

SQLProvider has only one method called getSql()and it is implemented using PreparedStatementCreator implementers. It is mainly used for debugging.

Bean wiring is the creation of associations between application components that are between the beans in a particular spring container.

If a bean element is directly embedded in a property tag while wiring beans, then the bean is called Inner Bean. Its drawback is that it cannot be reprocessed.

The web module enables the creation of a web application without XML. The web.xml file needs to be configured for using the web module.

Following are the channels supported by spring version 2.0:

  • Pollable Channel
  • Subscribable Channel
  • PublishSubscribe Channel
  • Queue Channel
  • Priority Channel
  • Rendezvous Channel
  • Direct Channel
  • Executor Channel
  • Scoped Channel

The major benefits of dependency injection or IOC are that it reduces the amount of coding required for the application. This allows the testing of the application to be done quickly and easily as no JNDI lookup mechanism or singletons are required. IOC containers also support lazy loading and eager installation of services.

Autoproxying is used to create proxy automatically for the spring users.

It provides following two classes to support this automatic proxy creation:

  • BeanNameAutoProxyCreator
  • DefaultAdvisorAutoProxyCreator

Scopes of spring bean are Singleton, prototype, request, session and global session.

Core container module is responsible for the basic functionality of the spring framework. The whole Spring framework is built with this module as a base.

The term proxy refers to an object which is produced the application of an advice to the target object.

There are four different types of Auto wire:

  • byName
  • byType
  • constructor
  • autodetect

Inversion of Control (IOC) is also called as dependency Injection which is nothingbut a design pattern that gives control to the assembler of classes. In general, class will instantiate another class if required.

But in this design pattern, this control has been to given to assembler and assembler will instantiate required class if needed.

Following are the points where weaving can be applied:

  • Compile Time
  • Class load Time
  • Runtime

Following are the types of transaction management that has been supported by spring:

  • declarative
  • programmatically

Following are the different types of AutoProxying:

  • BeanNameAutoProxyCreator
  • DefaultAdvisorAutoProxyCreator
  • Metadata autoproxying

  • ApplicationContext allows more than one config files to exist while BeanFactory only permits one.
  • ApplicationContext can print events to beans registered as listeners. This feature is not supported by BeanFactory.
  • ApplicationContext also provides support for application of lifecycle events, internationalization messages and validation and also provides services like EJB integration, remoting, JNDI access and scheduling. These features too are not supported by Bean Factory.

Spring configuration file is an XML file and it contains class information. It also describes how these classes are configured and interact with each other.

Metadata Autoproxying can be performed inspiring which can be driven by metadata. This is determined by source level attributes and keeps metadata inside the source code.

This maintains metadata in one place and mainly used for declarative transaction support.

Following are the classes that are used to control database connection:

  • Data Source Utils
  • SmartData Source
  • AbstractData Source
  • SingleConnection DataSource
  • DriverManager DataSource
  • TransactionAware DataSourceProxy
  • DataSource  TransactionManager

Bean Factory is core of the spring framework and, it is a Lightweight container which loads bean definitions and manages your beans.  Beans are configured using XML file and manage singleton defined bean. It is also responsible for life cycle methods and injects dependencies. It also removes adhoc singletons and factories.

This is a very important module and supplies various necessary services like EJB integration, remoting, JNDI access and scheduling. It transforms spring into a framework. It also broadens the idea of BeanFactory by application of lifecycle events, providing support for internationalization messages and validation.

The default scope of bean is Singleton for Spring framework.