Top 50 Core-spring Interview Questions You Must Prepare 23.Jul.2024

Q1. What Are The Different Points Where Weaving Can Be Applied?

Weaving can be done at compile time, load time, or at runtime.

Q2. What Is Target Object?

The object being advised by one or more aspects, this object will always be a proxy object. Also referred to as the advised object.

Q3. What Is Bean Auto Wiring?

The Spring container is able to autowire relationships between collaborating be. This me that it is possible to automatically let Spring resolve collaborators (other be) for your bean by inspecting the contents of the BeanFactory without using <constructor-arg> and <property> elements.

Q4. How Do You Provide Configuration Metadata To The Spring Container?

There are following three important methods to provide configuration metadata to the Spring Container:

  1. XML based configuration file.
  2. Annotation-based configuration
  3. Java-based configuration

Q5. What Is The Difference Between Concern And Cross-cutting Concern In Spring Aop?

Concern: Concern is behavior which we want to have in a module of an application. Concern may be defined as a functionality we want to implement. Issues in which we are interested define our concerns.

Cross-cutting concern: It's a concern which is applicable throughout the application and it affects the entire application. e.g. logging , security and data trfer are the concerns which are needed in almost every module of an application, hence are cross-cutting concerns.

Q6. How Is Event Handling Done In Spring?

Event handling in the ApplicationContext is provided through the ApplicationEvent class and ApplicationListener interface. So if a bean implements the ApplicationListener, then every time an ApplicationEvent gets published to the ApplicationContext, that bean is notified.

Q7. What Bean Scopes Does Spring Support? Explain Them.?

The Spring Framework supports following five scopes, three of which are available only if you use a web-aware ApplicationContext.

singleton: This scopes the bean definition to a single instance per Spring IoC container.

prototype: This scopes a single bean definition to have any number of object instances.

request: This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.

session: This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

global-session: This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

Q8. What Is Spring Java Based Configuration? Give Some Annotation Example.?

Java based configuration option enables you to write most of your Spring configuration without XML but with the help of few Java-based annotations.

For example: Annotation @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.

Q9. How Can You Inject Java Collection In Spring?

Spring offers four types of collection configuration elements which are as follows:

<list>: This helps in wiring i.e. injecting a list of values, allowing duplicates.

<set>: This helps in wiring a set of values but without any duplicates.

<map>: This can be used to inject a collection of name-value pairs where name and value can be of any type.

<props>: This can be used to inject a collection of name-value pairs where the name and value are both Strings.

Q10. Explain Bean Life Cycle In Spring Framework?

Following is sequence of a bean lifecycle in Spring:

Instantiate - First the spring container finds the bean's definition from the XML file and instantiates the bean..

Populate properties - Using the dependency injection, spring populates all of the properties as specified in the bean definition..

Set Bean Name - If the bean implements BeanNameAware interface, spring passes the bean's id to setBeanName() method.

Set Bean factory - If Bean implements BeanFactoryAware interface, spring passes the beanfactory to setBeanFactory() method.

Pre Initialization - Also called postprocess of bean. If there are any bean BeanPostProcessors associated with the bean, Spring calls postProcesserBeforeInitialization() method.

Initialize be - If the bean implements IntializingBean,its afterPropertySet() method is called. If the bean has init method declaration, the specified initialization method is called.

Post Initialization - If there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.

Ready to use - Now the bean is ready to use by the application.

Destroy - If the bean implements DisposableBean , it will call the destroy() method .

Q11. What Are The Benefits Of Ioc?

The main benefits of IOC or dependency injection are:

  1. It minimizes the amount of code in your application.
  2. It makes your application easy to test as it doesn't require any singletons or JNDI lookup mechanisms in your unit test cases.
  3. Loose coupling is promoted with minimal effort and least intrusive mechanism.
  4. IOC containers support eager instantiation and lazy loading of services.

Q12. What Are The Common Implementations Of The Applicationcontext?

The three commonly used implementation of 'Application Context' are:

FileSystemXmlApplicationContext: This container loads the definitions of the be from an XML file. Here you need to provide the full path of the XML bean configuration file to the constructor.

ClassPathXmlApplicationContext: This container loads the definitions of the be from an XML file. Here you do not need to provide the full path of the XML file but you need to set CLASSPATH properly because this container will look bean configuration XML file in CLASSPATH.

WebXmlApplicationContext: This container loads the XML file with definitions of all be from within a web application.

Q13. What Is Aop?

Aspect-oriented programming, or AOP, is a programming technique that allows programmers to modularize crosscutting concerns, or behavior that cuts across the typical divisions of responsibility, such as logging and traction management. The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules.

Q14. Describe Some Of The Standard Spring Events.?

Spring provides the following standard events:

ContextRefreshedEvent: This event is published when the ApplicationContext is either initialized or refreshed. This can also be raised using the refresh() method on the ConfigurableApplicationContext interface.

ContextStartedEvent: This event is published when the ApplicationContext is started using the start() method on the ConfigurableApplicationContext interface. You can poll your database or you can re/start any stopped application after receiving this event.

ContextStoppedEvent: This event is published when the ApplicationContext is stopped using the stop() method on the ConfigurableApplicationContext interface. You can do required housekeep work after receiving this event.

ContextClosedEvent: This event is published when the ApplicationContext is closed using the close() method on the ConfigurableApplicationContext interface. A closed context reaches its end of life; it cannot be refreshed or restarted.

RequestHandledEvent: This is a web-specific event telling all be that an HTTP request has been serviced.

Q15. What Is Dependency Injection?

Inversion of Control (IoC) is a general concept, and it can be expressed in many different ways and Dependency Injection is merely one concrete example of Inversion of Control.

This concept says that you do not create your objects but describe how they should be created. You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (the IOC container) is then responsible for hooking it all up.

Q16. What Is Join Point?

This represents a point in your application where you can plug-in AOP aspect. You can also say, it is the actual place in the application where an action will be taken using Spring AOP framework.

Q17. What Are Benefits Of Using Spring?

Following is the list of few of the great benefits of using Spring Framework:

Lightweight: Spring is lightweight when it comes to size and trparency. The basic version of spring framework is around 2MB.

Inversion of control (IOC): Loose coupling is achieved in spring using the technique Inversion of Control. The objects give their dependencies instead of creating or looking for dependent objects.

Aspect oriented (AOP): Spring supports Aspect oriented programming and enables cohesive development by separating application business logic from system services.

Container: Spring contains and manages the life cycle and configuration of application objects.

MVC Framework: Spring's web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks such as Struts or other over engineered or less popular web frameworks.

Traction Management: Spring provides a consistent traction management interface that can scale down to a local traction (using a single database, for example) and scale up to global tractions (using JTA, for example).

Exception Handling: Spring provides a convenient API to trlate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO, for example) into consistent, unchecked exceptions.

Q18. What Are Types Of Ioc Containers? Explain Them.

There are two types of IoC containers:

Bean Factory container: This is the simplest container providing basic support for DI .The BeanFactory is usually preferred where the resources are limited like mobile devices or applet based applications

Spring ApplicationContext Container: This container adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners.

Q19. What Is Webapplicationcontext ?

The WebApplicationContext is an extension of the plain ApplicationContext that has some extra features necessary for web applications. It differs from a normal ApplicationContext in that it is capable of resolving themes, and that it knows which servlet it is associated with.

Q20. Explain Requestmapping Annotation.?

RequestMapping annotation is used to map a URL to either an entire class or a particular handler method.

Q21. What Is Default Scope Of Bean In Spring Framework?

The default scope of bean is Singleton for Spring framework.

Q22. What Is Introduction?

An introduction allows you to add new methods or attributes to existing classes.

Q23. What Does

This annotation simply indicates that the affected bean property must be populated at configuration time, through an explicit property value in a bean definition or through autowiring. The container throws BeanInitializationException if the affected bean property has not been populated.

Q24. What Is Spring Ioc Container?

The Spring IoC creates the objects, wire them together, configure them, and manage their complete lifecycle from creation till destruction. The Spring container uses dependency injection (DI) to manage the components that make up an application.

Q25. What Are The Types Of Advice?

Spring aspects can work with five kinds of advice mentioned below:

before: Run advice before the a method execution.

after: Run advice after the a method execution regardless of its outcome.

after-returning: Run advice after the a method execution only if method completes successfully.

after-throwing: Run advice after the a method execution only if method exits by throwing an exception.

around: Run advice before and after the advised method is invoked.

Q26. What Is Spring Mvc Framework?

The Spring web MVC framework provides model-view-controller architecture and ready components that can be used to develop flexible and loosely coupled web applications.

The MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements.

Q27. What Are Inner Be In Spring?

A <bean/> element inside the <property/> or <constructor-arg/> elements defines a so-called inner bean. An inner bean definition does not require a defined id or name; the container ignores these values. It also ignores the scope flag. Inner be are always anonymous and they are always scoped as prototypes.

Q28. Give An Example Of Beanfactory Implementation.?

The most commonly used BeanFactory implementation is the XmlBeanFactory class. This container reads the configuration metadata from an XML file and uses it to create a fully configured system or application.

Q29. What Is Annotation-based Container Configuration?

An alternative to XML setups is provided by annotation-based configuration which relies on the bytecode metadata for wiring up components instead of angle-bracket declarations. Instead of using XML to describe a bean wiring, the developer moves the configuration into the component class itself by using annotations on the relevant class, method, or field declaration.

Q30. What Are The Jsr-250 Annotations? Explain Them.?

Spring has JSR-250 based annotations which include @PostConstruct, @PreDestroy and @Resource annotations.

@PostConstruct: This annotation can be used as an alternate of initialization callback.

@PreDestroy: This annotation can be used as an alternate of destruction callback.

@Resource : This annotation can be used on fields or setter methods. The @Resource annotation takes a 'name' attribute which will be interpreted as the bean name to be injected. You can say, it follows by-name autowiring semantics.

Q31. Are Singleton Be Thread Safe In Spring Framework?

No, singleton be are not thread-safe in Spring framework.

Q32. What Is Spring Configuration File?

Spring configuration file is an XML file. This file contains the classes information and describes how these classes are configured and introduced to each other.

Q33. What Does A Bean Definition Contain?

The bean definition contains the information called configuration metadata which is needed for the container to know the followings:

  1. How to create a bean
  2. Bean's lifecycle details
  3. Bean's dependencies

Q34. How Jdbctemplate Can Be Used?

With use of Spring JDBC framework the burden of resource management and error handling is reduced a lot. So it leaves developers to write the statements and queries to get the data to and from the database.

JdbcTemplate provides many convenience methods for doing things such as converting database data into primitives or objects, executing prepared and callable statements, and providing custom database error handling.

Q35. What Is Weaving?

Weaving is the process of linking aspects with other application types or objects to create an advised object.

Q36. How Jdbc Can Be Used More Efficiently In Spring Framework?

JDBC can be used more efficiently with the help of a template class provided by spring framework called as JdbcTemplate.

Q37. How Do You Turn On Annotation Wiring?

Annotation wiring is not turned on in the Spring container by default. So, before we can use annotation-based wiring, we will need to enable it in our Spring configuration file by configuring <context:annotation-config/>.

Q38. What Are The Types Of The Traction Management Spring Supports?

Spring supports two types of traction management:

Programmatic traction management: This me that you have managed the traction with the help of programming. That gives you extreme flexibility, but it is difficult to maintain.

Declarative traction management: This me you separate traction management from the business code. You only use annotations or XML based configuration to manage the tractions.

Q39. What Are Spring Be?

The objects that form the backbone of your application and that are managed by the Spring IoC container are called be. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These be are created with the configuration metadata that you supply to the container, for example, in the form of XML <bean/> definitions.

Q40. What Are The Ways To Access Hibernate By Using Spring?

There are two ways to access hibernate using spring:

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

Q41. What Is A Dispatcherservlet?

The Spring Web MVC framework is designed around a DispatcherServlet that handles all the HTTP requests and responses.

Q42. What Are The Different Modules In Spring Framework?

Following are the modules of the Spring framework:

  • Core module
  • Bean module
  • Context module
  • Expression Language module
  • JDBC module
  • ORM module
  • OXM module
  • Java Messaging Service(JMS) module
  • Traction module
  • Web module
  • Web-Servlet module
  • Web-Struts module
  • Web-Portlet module

Q43. Explain The

The @Controller annotation indicates that a particular class serves the role of a controller. Spring does not require you to extend any controller base class or reference the Servlet API.

Q44. Which Di Would You Suggest Constructor-based Or Setter-based Di?

Since you can mix both, Constructor- and Setter-based DI, it is a good rule of thumb to use constructor arguments for mandatory dependencies and setters for optional dependencies. Note that the use of a @Required annotation on a setter can be used to make setters required dependencies.

Q45. What Are Different Modes Of Auto Wiring?

The autowiring functionality has five modes which can be used to instruct Spring container to use autowiring for dependency injection:

no: This is default setting which me no autowiring and you should use explicit bean reference for wiring. You have nothing to do special for this wiring. This is what you already have seen in Dependency Injection chapter.

byName: Autowiring by property name. Spring container looks at the properties of the be on which autowire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the be defined by the same names in the configuration file.

byType: Autowiring by property datatype. Spring container looks at the properties of the be on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the be name in configuration file. If more than one such be exist, a fatal exception is thrown.

constructor: Similar to byType, but type applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.

autodetect: Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType.

Q46. What Are Orm's Spring Supports ?

Spring supports the following ORM's :

  • Hibernate
  • iBatis
  • JPA (Java Persistence API)
  • TopLink
  • JDO (Java Data Objects)
  • OJB

Q47. How Do Add A Bean In Spring Application?

Check the following example:

<?xml version="1.0" encoding="UTF-8"?>

<be xmlns=""

    xmlns:xsi=""    xsi:schemaLocation="">

   <bean id="helloWorld" class="com.tutorialspoint.HelloWorld">

       <property name="message" value="Hello World!"/>



Q48. What Is Spring?

Spring is an open source development framework for enterprise Java. The core features of the Spring Framework can be used in developing any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring framework targets to make J2EE development easier to use and promote good programming practice by enabling a POJO-based programming model.

Q49. What Are The Limitations With Autowiring?

Limitations of autowiring are:

Overriding possibility: You can still specify dependencies using <constructor-arg> and <property> settings which will always override autowiring.

Primitive data types: You cannot autowire so-called simple properties such as primitives, Strings, and Classes.

Confusing nature: Autowiring is less exact than explicit wiring, so if possible prefer using explicit wiring.

Q50. What Is Pointcut?

This is a set of one or more joinpoints where an advice should be executed. You can specify pointcuts using expressions or patterns as we will see in our AOP examples.