Select Page

Spring Framework Interview Questions & Answers

Hello guys in this blog of services I will discuss the frequently asked the most popular Spring framework interview questions.

1>What is the Spring Framework?

  • Spring is an open-source application framework which is used to reduce complexities to develop JEE (Java enterprise application)
  • Spring is a lightweight and loosely coupled framework.
  • Through the Spring framework, we can connect to other frameworks like Hibernate, Struts, and JSF, etc.
  • Spring Framework provides layered architecture. We can select the component to create to use when we work with the JEE application.

2>What are the different features of the Spring Framework?

  • AOP(Aspect Oriented Programming): Aop in the Spring framework is used for separation of concerns. Spring supports a cohesive approach which means we can separate business logic and secondary concerns like transactions, logging from our business logic.
  • MVC Framework: Spring MVC web framework is configurable. By using this framework we can easily connect with other frameworks.
  • Spring Container: Spring container is responsible for creating and managing the lifecycle of the component. The job of the container to provide support of DI.
  • Inversion of Control: IOC is used to achieve dependencies in Object-oriented programming. Inversion of control is achieved through the dependency injection mechanism.
  • Transaction Management: Spring framework provides an abstract way to manage the transaction.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

3>What is dependency injection? how do we implement DI in the spring framework?

Independency Injection is a specific form of Inversion of Control in which we do not have to create your objects but we have to describe how the dependent object should be created and IOC container will wire dependency object to dependant one.

In the Spring framework, we can use XML and annotation configuration to implement DI in the Spring application. Dependency is provided through constructor or property injection.

4>What is the feature of Spring 5?

Ans: The new features of the Spring framework is as follows:

  • Spring uses Java 8 features and Java EE 7 features Servlet 4 .0 feature it included in Spring5.
  • Spring Framework has its own common-loggings bridge instead of common logging.
  • In the Spring framework, it will provide component information through the index file “META-INF/spring. components “ instead of scanning classpath
  • In Spring 5 WebFlux module is used for reactive programming in the Spring framework.
  • Spring5 support Kotlin programming to support functional.
  • It will support JUnit5 and testing execution in the TestContext framework.

5>What is Spring Web Flux?

Ans: Spring Web Flux is a new module is introduced in Spring5 for the reactive programming model in the Spring framework. 

Basically it is an alternative to the Spring MVC module. By using the Spring WebFlux module we can create a fully asynchronous and non-blocking application built on the event-loop model.

6> What is Aspect, Advice, Pointcut, JoinPoint and Advice Arguments in AOP?

  • Aspect: In the AOP aspect represent cross-cutting concerns e.g. Logging, Transaction, etc. To define Aspect we write a class. This class is configured with Spring Bean configuration file or Spring Aspect support using @Aspect Annotation.
  • Advice: Advice define what and when of Aspect. Advice is an action that is taken for a particular join point. In the programming, they are methods that get executed when a specific join point with matching pointcut is reached in the application. 
  • Pointcut: In AOP Pointcut is a regular expression that is matched with join points to determine whether advice needs to be executed or not. Spring framework uses AspectJ pointcut expression language for checking the join points where advice methods will be applied.
  • JoinPoint: In the application, the joining point defines the specific point of execution, exception handling and change in the object variable values. Spring support execution method join point.

7>What is the difference between Spring AOP and AspectJ AOP?

Ans: AspectJ is the industry-standard implementation for Aspect-Oriented Programming. Spring AOP is for some cases. The difference between both is as follows.

  • Spring AOP is simple to use as compare to AspectJ because we need not worry about the weaving of Aspect
  • Spring AspectJ framework is based on Annotation hence if we familiar with Spring AspectJ then it will vary to learn Spring AOP.
  • Spring AOP is supported only method execution join points as it supports proxy-based AOP. AspectJ supports all kinds of pointcuts.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

8>What is Spring IOC Container?

Spring IOC container is used for loop coupling between the application object dependencies. In Spring framework IOC container inject program dependencies into an object at runtime. For that, the Objects define their dependencies that injecting and make it ready for use. Spring IOC container classes ApplicationContext implementation class is 

1>AnnotationConfigApplicationContext: It is used for Java application using Annotation-based configuration.

2>ClassPathXmlApplication :It is used for standalone application using XML based configuration.

3>FileSystemXmlApplicationContext:This class is similar to ClassPathXmlApplicationcontext except that it load XML configuration file anywhere from File system.

4>AnnotationConfigWebApplicationContext and XmlWebApplicationContext  is used for web application.

8>What is Spring Bean?

Ans: Spring Bean is a simple Java class that is initialized by Spring container. The job of Spring container is used to create and manage the life cycle of Spring Bean. Spring container will take about managing dependencies at a time of Spring Bean creation.

9>What are the different ways to configure the bean?

Ans: Spring frameworks provide several ways to configure the Bean. There are three ways to configure which is as follows:

1>XML configuration: XML configuration is the most common way to map bean in the spring framework. E.g

        <bean id=”emp” class=”com.sevenmentor.Employee”/>

2>Java based configuration : Java based configuration is possible through Annotations .

 @Bean , @Configuration and @ComponentScan  is used.

Java based configuration

In this case, get a bean from spring-context following code is used.

3> Annotation Based Configuration: This is a programmatic approach in which java classes are annotated with  @Component, @Service, @Repository, and @Controller annotations to configure them to be as spring bean. For this, we need to scan classes for packages.

10> What are the different scopes of Spring Bean?

Ans: There are five scopes are defined for Spring Beans which are as follows.

1>singleton: In this scope, only one instance of the bean will be created for each container. This is the default scope. When we use this scope then spring bean doesn’t have shared instance variables otherwise data inconsistency issues may raise.

2>prototype: In this scope, every time new bean is created when it is requested.

3>request: This scope is used in web-based application it is the same as the prototype , a new request object is created when a new instance of the bean will be created for each client request.

4>session: For every HttpSession a new bean is created by the container.

5>global-session: It is used to create a global session for Portlet application.

In spring application we can create our own scope but the recommendation is to use scope given by spring container. Spring XML configuration we use “scope” attribute and @Scope is used for Annotation-based configuration.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

11> What is the difference between @Component, @Controller, @Repository & @Service annotations in Spring?

Ans: @Component: This annotation denotes the class is a component. The classes which are annotated by @Component they used for spring auto-detection and the class is configured as a bean .

@Controller is used in the Spring MVC application for RequestMapping for a specific type of component.

@Repository: This Annotation is used for DAO repository classes for a mechanism for the store, retrieve and searches data.

@Service: It is used to service classes of the application.

12>  Differentiate between BeanFactory and ApplicationContext.

13> What is the Bean life cycle in Spring Bean Factory Container?

Bean life cycle in Spring Bean Factory Container has the following Steps 

1> Spring container instantiates bean from bean definition through XML for spring auto-detect mechanism.

2>Spring then populate all properties using DI (dependency Injection ) as per defined in XML or Annotation-based configuration,

3> After DI then factory calls setBeanName() by passing beans id , provided that bean class BeanNameAware interface.

4> Spring Factory calls setBeanFactory() by passing an instance of itself if the bean class implements the BeanFactoryAware interface.

5> If  our bean class  implements BeanPostProcessors interface Spring call preProcessBeforeInitialization()  .

6> In our spring application if provide custom initialization then Spring framework call that.

7> Then finally, postProcessAfterInitialization() methods will be called if there are any BeanPostProcessors associated with the bean.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

14> What do you understand by auto wiring and name the different modes of it?

Ans: In the Spring framework when beans are combined together with its dependency it is called the wiring of the bean. Spring container is implemented auto-wire relationships between beans it can be done with different modes of auto wiring such as follows 

1>No: This is the default type of auto wiring in this bean. Need to map explicit reference for wiring.

2>byName: It will Inject dependency according to bean name. It will automatically match the bean with the same name in XML and get wired.

3>byType: It will  Inject dependency according to bean type. I will automatically match the bean by its type.

4>Constructor: It will inject dependency by calling the constructor of the class.

5>Autodetect: In this case container first match by the constructor and if it can’t found then it will auto-wire property by byName.

Jayashree Chaudhary |SevenMentor Pvt Ltd

Call the Trainer and Book your free demo class for JAVA now!!!

call icon

© Copyright 2020 | Sevenmentor Pvt Ltd.


Pin It on Pinterest

× How can I help you?