The following are the Spring Interview Questions which discuss the top 50+ questions with proper explanation.
Spring is set to be a framework that helps Java programmer for the development of code and it provides IOC container, Dependency Injector, MVC flow and many other APIs for the java programmer.
It is the execution of an aspect. Advice is like making your application learn a new trick. They are usually introduced at joinpoints.
The default scope of the bean is Singleton for the Spring framework.
Transaction management supported by Spring are :
Declarative transaction management.
Programmatic transaction management.
Q.5- Is Singleton beans are a thread-safe in Spring Framework?
Ans- No, singleton beans are not thread-safe in the Spring framework.
Q.6- What are the benefits of the Spring Framework?
Ans- Following are the benefits of the Spring framework:
- Extensive usage of Components
- Reduces coding effort by using pattern implementations such as singleton, factory, service locator, etc.
- Removal of leaking connections
- Declarative transaction management
- Easy to integrate with third-party tools and technologies.
Q.7- What is Bean Factory?
Ans- Bean Factory is the core of the spring framework and, it is a Lightweight container that 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.
Q.8- Define Bean Wiring?
Ans- Bean wiring is the creation of associations between application components that are between the beans in a particular spring container.
Q.9- What is called Spring MVC?
Ans- A Spring MVC is a single shared controller instance and it is used to handle request type controllers, interceptors that run in the IoC container. It also allows multiple Dispatcher Servlets which can share the application context interface but not the class-based interface.
Q.10- What are the different types of events of Listeners?
Ans- Following are the different types of events of listeners:
- ContextClosedEvent – This event is called when the context is closed.
- ContextRefreshedEvent – This event is called when the context is initialized or refreshed
- RequestHandledEvent – This event is called when the web context handles the request
Q.11- Differentiate between singleton and prototype bean?
Ans- Singleton means only one bean is defined per object instance while Prototype means one definition to more than one object instances in Spring.
Q.12- What are the types of Dependency Injection?
Ans- Two types of dependency injection are supported by the spring framework:
- Setter Injection
- Constructor Injection
Q.13- Write about the Core container module?
Ans- The 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.
Q.14- What is the AOP module?
Ans- This AOP module is used for spring enabled applications. Support has been provided AOP alliance to ensure the interoperability between spring and other AOP frameworks.
It instructs spring to add annotations to the source code and tell how to apply aspects.
Q.15- What is AOP Alliance?
Ans- AOP Alliance is an open-source project which is aimed at promoting the adoption of AOP. The AOP alliance’s goal is to define a common set of components and interfaces so as to improve interoperability among different AOP implementations.
Q.16- What is called the spring configuration file?
Ans- Spring configuration file is an XML file and it contains classified information. It also describes how these classes are configured and interact with each other.
Q.17- What are different types of Autowire?
Ans- There are four different types of Auto wire:
Q.19- What are the types of transaction management that are supported by spring?
Ans- Following are the types of transaction management that have been supported by spring:
Q.20- When are declarative and programmatic transaction management used?
Ans- 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.
Q.21- Why the Spring framework is needed?
Ans- Spring framework is needed because it is –
- Very Light Weight Container
Q.22- Name the various modules used in the spring framework?
- AOP module (Aspect Oriented Programming)
- JDBC abstraction and DAO module
- The Core container module
- MVC framework module
- Application context module
- O/R mapping integration module (Object/Relational)
- Web module
Q.23- Explain the RowCallbackHandler in Spring?
Ans- The RowCallbackHandler is called for each row in ResultSet and is used to read values from the ResultSet.
Q.24- Define Application context module?
Ans- 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.
Q.25- Write about the AOP module?
Ans- AOP module is utilized for creating aspects for Spring applications. It also enables support for metadata programming in Spring.
Q.26- What is a BeanFactory Interface?
Ans- Bean factory interface is used to provide the configuration framework for object creation and basic functionality around object management.
Q.27- State the differences between ApplicationContext and BeanFactory in spring?
Ans- ApplicationContext allows more than one config file 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 the 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.
Q.28- What is Auto Wiring?
Ans- Autowiring is used to build relationships between the collaborating beans. Spring container can automatically resolve collaborators for beans.
Q.29- What are the different Modes of Autowiring?
Ans- Autowiring has five different modes:
- no: no autofire
- byName : Autowiring that can be done by the property name
- byType : property type as autowired
- constructor: It is similar to byType and it is property is in the constructor
- autodetect: Spring is allowed to select auto wiring from byType or constructor
Q.30- How to start using spring?
Ans- Following steps need to be done to start with the Spring:
- Download Spring and its dependent file from spring’s site.
- Create the application context XML to define beans and its dependencies
- Integrate application context XML with web.xml
- Deploy and Run the application
Q.31- What are the methods of the bean life cycle?
Ans- There are two important methods of the Bean life cycle:
- Setup – called when the bean is loaded into the container
- Teardown – called when the bean is unloaded into the container
Q.32- What is IOC?
Ans- IOC (Inversion of Control pattern) is also known as dependency injection. IOC directs the programmers to depict how to create objects instead of actually creating them. But in this design pattern, this control has been given to assembler and assembler will instantiate required class if needed.
Q.33- Write about the different types of Listener related events?
Ans- The different types of events related to listeners are:
- ContextRefreshedEvent – This gets called when the context is refreshed or initialized.
- RequestHandledEvent – This gets called when the web context is handling a request.
- ContextClosedEvent – This gets called when the context gets closed.
Q.34- What is an Aspect?
Ans- Aspect is also called logging which is required throughout the application. Logging or aspect is a cross-cutting functionality in an application using AOP.
Q.35- What is a Joinpoint?
Ans- 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 the normal flow of the application to change the current behavior.
Q.36- What is called an Advice?
Ans- 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 new behavior. Generally, the advice is inserted into an application at joinpoints.
Q.37- What is a Pointcut?
Ans- Pointcut is used to allow where the advice can be applied.
Q.38- What is weaving?
Ans- Weaving is used to creating a new proxy object by applying aspects to the target object.
Q.39- What is the difference between singleton and prototype bean?
Ans- 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
Q.40- In what points, can weaving be applied?
Ans- Following are the points where weaving can be applied:
- Compile Time
- Class load Time
Q.41- What are the different types of AutoProxying?
Ans- Following are the different types of AutoProxying:
- Metadata autoproxying
Q.42- How can beans be made singleton or prototype?
Ans- The bean tag has an attribute called ‘singleton’. The bean is singleton if its value is ‘TRUE’, otherwise the bean is a prototype.
Q.43- What classes are used to control the database connection?
Ans- Following are the classes that are used to control the database connection:
- Data Source Utils
- SmartData Source
- AbstractData Source
- SingleConnection DataSource
- DriverManager DataSource
- TransactionAware DataSourceProxy
- DataSource TransactionManager
Q.44- Describe about DAO in Spring framework?
Ans- DAO is used to provide integration of Java database connectivity and Object-relational mapping objects. DAO is a spring framework provides a connection for JDBC, hibernate, JDO, JPA, Common client interface and Oracle.
Q.45- What is Autoproxying?
Ans- Autoproxying is used to create proxy automatically for the spring users. It provides the following two classes to support this automatic proxy creation:
Q.46- What is Metadata Autoproxying?
Ans- 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.
Q.47- What is ‘Throws advice’ in Spring?
Ans- ‘Throws Advice’ defines the behavior when an exception occurs. It is an interface and it has no methods which need to be implemented.
A class that implements this interface should have a method with this signature:
- Void samplethrow (Throw tablet)
- Void samplethrow(Method m, Object o, Object target, Throw tablet)
Q.48- What are the various editors used in spring work?
Ans- The various custom editors provided by the Spring Framework are:
Q.49- What are the advantages of the spring framework?
Ans- Following are the advantages of the 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
Q.50- How is Hibernate accessed using the Spring framework?
Ans- Hibernate can be accessed in the following two ways:
- By IOC with a Callback and Hibernate Template.
- By applying an AOP Interceptor and broadening the HibernateDaoSupport.
Q.51- What are the various Channels supported by Spring 2.0?
Ans- 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
Q.52- Why is declarative transaction management preferred in Spring?
Ans- Declarative transaction management has minimum impact on the application code and, therefore, is an idealistic lightweight container.
Q.53- Explain the concept of a BeanFactory?
Ans- BeanFactory applies the idea of a factory pattern that utilizes IOC to separate the application’s dependencies and configuration from the actual code.
Q.54- What are the different scopes of spring bean?
Ans- Scopes of spring bean are Singleton, prototype, request, session, and global session.
Q.55- What are all the ways to access Hibernate by using Spring?
Ans- 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.
Q.56- How struts application can be integrated with spring?
Ans- There are two options for struts application that can be integrated with spring:
Configuration of Spring to manage beans using ContextLoader plugin and set their dependencies in a spring context file
Grab spring managed beans explicitly using agetwebapplicationcontext()
Q.57- What is Inversion of control (IOC)?
Ans- Inversion of Control (IOC) is also called as dependency Injection which is nothing but a design pattern that gives control to the assembler of classes. In general, the 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.
Q.58- Write the benefits of using IOC?
Ans- 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.
Q.59- What is Inner bean? What is the drawback of inner bean?
Ans- 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.
Q.60- What are the different types of Injection in spring?
Ans- There are three types of Injection in spring:
- Setter Injection
- Constructor Injection
- Getter or Method Injection
Q.61- What are the benefits of the spring framework?
Ans- Following are the benefits of spring framework:
- Lightweight container when compared to j2EE containers
- Built-in Web MVC framework
- Creates loosely coupled applications
- Supports aspect-oriented programming like logging, transaction, and security
- The configuration is done in XML format which is easy to write and understand
Q.62- What are the types of Advice?
Ans- There are five types of Advice:
- Before Advice
- After returning advice
- After throwing advice
- Finally advice
- Around advice
Q.63- What is called PreparedStatementCreator?
Ans- PreparedStatementCreator is one of the most commonly used interfaces for writing data to the database. createPreparedStatement() is a method that can be used to create and return PreparedStatement from the Connection argument, and exception handling is automatically taken care of. When this interface is implemented, a different interface SqlProvider can also be implemented which has a method called getSql(). This method is useful for providing SQL strings to the JdbcTemplate. It does not handle SQLExceptions.
Q.64- What is SQLProvider?
Ans- SQLProvider has only one method called getSql()and it is implemented using PreparedStatementCreator implementers. It is mainly used for debugging.