Spring Interview Questions and Answer for Experienced
Categories: Interview questions and answers Experienced Freshers Spring
Question:- What is Spring Framework?
Answer :-Spring is a powerful open-source, loosely coupled, lightweight, java framework meant for reducing the complexity of developing enterprise-level applications. This framework is also called the “framework of frameworks” as spring provides support to various other important frameworks like JSF, Hibernate, Structs, EJB, etc.
There are around 20 modules which are generalized into the following types:
- Core Container
- Data Access/Integration
- AOP (Aspect Oriented Programming)
Answer :- The main idea in Dependency Injection is that you don’t have to create your objects but you just have to describe how they should be created.
- The components and services need not be connected by us in the code directly. We have to describe which services are needed by which components in the configuration file. The IoC container present in Spring will wire them up together.
- In Java, the 2 major ways of achieving dependency injection are:
- Constructor injection: Here, the IoC container invokes the class constructor with a number of arguments where each argument represents a dependency on the other class.
- Setter injection: Here, the spring container calls the setter methods on the beans after invoking a no-argument static factory method or default constructor to instantiate the bean.
Question:- Explain the difference between constructor and setter injection?Answer :-
- In constructor injection, partial injection is not allowed whereas it is allowed in setter injection.
- The constructor injection doesn’t override the setter property whereas the same is not true for setter injection.
- Constructor injection creates a new instance if any modification is done. The creation of a new instance is not possible in setter injection.
- In case the bean has many properties, then constructor injection is preferred. If it has few properties, then setter injection is preferred.
Question:- What is the Bean life cycle in Spring Bean Factory Container?
Answer :- 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:- What do you understand by auto wiring and name the different modes of it?
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:
a. no: This is default setting which means no autowiring. Explicit bean reference should be used for wiring.
b. 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.
c. 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.
d. constructor: It injects the dependency by calling the constructor of the class. It has a large number of parameters.
e. autodetect: First the container tries to wire using autowire by constructor, if it can’t then it tries to autowire by byType.
Question:- What are the limitations with auto wiring?
Answer :- Following are some of the limitations you might face with auto wiring:
- Overriding possibility: You can always specify dependencies using <constructor-arg> and <property> 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:- What are the bean scopes available in Spring?
- Answer :- The Spring Framework has five scope supports. They are:
- Singleton: The scope of bean definition while using this would be a single instance per IoC container.
- Prototype: Here, the scope for a single bean definition can be any number of object instances.
- Request: The scope of the bean definition is an HTTP request.
- Session: Here, the scope of the bean definition is HTTP-session.
- Global-session: The scope of the bean definition here is a Global HTTP session.
Question:- What is Annotation-based container configuration? Also, explain how to turn on annotation wiring in Spring Framework.
Answer :- Annotation-based container configuration is an alternative to XML setups. Rather than using XML for describing a bean wiring, the developer moves the configuration to the component class by using annotations on the appropriate class, field, or method declaration.
Because annotation wiring is turned off by default, it needs to be turned on before it can be used. It is done by configuring the <context:annotation-config/> element in the Spring configuration file.
Question:- What do you mean by Spring DAO support?
Answer :- The Spring DAO support eases working with data access technologies, such as JDBC, JDO, and Hibernate, in a reliable way. Also, it allows coding without worrying about catching specific-technology exceptions and easily makes a switch amongst persistence technologies.
Question:- What do you understand by Bean Wiring.
- When beans are combined together within the Spring container, they are said to be wired or the phenomenon is called bean wiring.
- The Spring container should know what beans are needed and how the beans are dependent on each other while wiring beans. This is given by means of XML / Annotations / Java code-based configuration.
Question:- What is Advice in Spring? Explain its various types.
Answer :- Any action taken by an aspect at some particular joinpoint in Spring Framework is called an Advice. Spring AOP makes use of advice for maintaining a chain of interceptors “around” the joinpoint i.e. as an interceptor. Advice can be of the following types:
- After (finally) – Configured using the @After annotation mark, it is executed after a joinpoint method, whether exiting normally or throwing an exception
- After returning – Configured using the @AfterReturning annotation mark, it is executed right after the joinpoint method completes normal execution
- After throwing – Configured using the @AfterThrowing annotation mark, it is executed if and only if the jointpoint method exits by throwing an exception
- Around – Configured using the @Around annotation mark, it is executed before as well as after a joinpoint method
- Before – Configured using the @Before annotation mark, it is executed before the joinpoint method
Question:- Could you draw a comparison between concern and crosscutting concerns in Spring AOP?
Answer :- While the concern is a behavior that the developer wants to have in a particular module of a Spring application, the cross-cutting concern is a concern that is applicable throughout the entire Spring application.
Question:- What do you understand by the term ‘Spring Boot’?
Answer :- Spring Boot is an open-source, java-based framework that provides support for Rapid Application Development and gives a platform for developing stand-alone and production-ready spring applications with a need for very few configurations.
Question:- Differentiate between Spring and Spring Boot.
- The Spring Framework provides multiple features like dependency injection, data binding, aspect-oriented programming (AOP), data access, and many more that help easier development of web applications whereas Spring Boot helps in easier usage of the Spring Framework by simplifying or managing various loosely coupled blocks of Spring which are tedious and have a potential of becoming messy.
- Spring boot simplifies commonly used spring dependencies and runs applications straight from a command line. It also doesn’t require an application container and it helps in monitoring several components and configures them externally.
Question:- What is Hibernate ORM Framework?
- Object-relational mapping (ORM) is the phenomenon of mapping application domain model objects to the relational database tables and vice versa.
- Hibernate is the most commonly used java based ORM framework.
Question:- What is the Spring MVC framework?
- Spring MVC is request driven framework and one of the core components of the Spring framework.
- It comes with ready to use loosely coupled components and elements that greatly aids developers in building flexible and robust web applications.
- The MVC (Model - View - Controller) architecture separates and provides loose coupling between the different aspects of the application – input logic (Model), business logic (Controller), and UI logic (View).
Question:- What are the ways by which Hibernate can be accessed using Spring?
Answer :- There are two ways by which we can access Hibernate using Spring:
a. Inversion of Control with a Hibernate Template and Callback.
b. Extending HibernateDAOSupport and Applying an AOP Interceptor node.
Question:- What is a Pointcut in Spring?
Answer :- Pointcut is an expression language of Spring AOP.
Question:- What are the different latest versions of Spring framework?
The latest versions of the Spring framework are as follows.
- Spring 2.5
- Spring 3.0
- Spring 4.0
- Question:- What is weaving in the Spring framework?
- Answer :- Weaving in Spring framework is the process of linking a particular aspect with other application types or objects so that an advised object is created. It is performed mostly during Runtime.
Question:- What is the role of @ModelAttribute annotation?
Answer :- The annotation plays a very important role in binding method parameters to the respective attribute that corresponds to a model. Then it reflects the same on the presentation page. The role of the annotation also depends on what the developer is using that for. In case, it is used at the method level, then that method is responsible for adding attributes to it. When used at a parameter level, it represents that the parameter value is meant to be retrieved from the model layer.
Question:- Can Spring Boot allow Spring MVC or Spring WebFlux in the same application?
Answer :- Yes, Spring Boot can allow either Spring MVC or Spring WebFlux in the same application but with the condition to apply only one at a time. This is because MVC is a blocking paradigm, and WebFlux is a non-blocking paradigm and hence cannot be used together.