Menu Close

spring interview questions

What is Spring?

  • Spring is an open source development framework.
  • The Spring framework is a Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure part so you can focus on your application part.
  • Spring framework targets to make J2EE development easier with POJO-based programming model.

Which are the Spring framework modules?

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

What are the benefits of using Spring Framework?

  • Dependency Injection(DI) approach makes the dependencies are explicit and evident in constructor or JavaBean properties.
  • IoC containers tend to be lightweight especially when compared to EJB containers.
  • Spring truly makes use of some of the existing technologies like ORM frameworks, logging frameworks, JEE, Quartz and JDK timers, other view technologies.
  • 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.
  • Spring provides a consistent transaction management interface that can scale down to a local transaction (using a single database, for example) and scale up to global transactions (using JTA).

What is Inversion of Control (IoC) and Dependency Injection?

  • Inversion of control is a design paradigm with the goal of giving more control to the targeted components of your application, the ones that are actually doing the work.
  • Dependency injection is a pattern used to create instances of objects that other objects rely on without knowing at compile time which class will be used to provide that functionality.

How we configure Spring into our application?

  • XML Based Configuration
  • Annotation-based configuration
  • Java-based configuration

What is Java-Based Spring Configuration?

  • We use @Configuration annotated classes and @Bean annotated methods for spring configuration.
  • The @Bean annotation is used to indicate that a method instantiates, configures and initializes a new object to be managed by the Spring IoC container.
  • The @Configuration indicates that its primary purpose is as a source of bean definitions.

What are inner beans in Spring?

  • In Spring framework, whenever a bean is used for only one particular property, it’s advise to declare it as an inner bean.
  • And the inner bean is supported both in setter injection ‘property’ and constructor injection ‘constructor-arg’.

How can you inject a Java Collection in Spring?

  • <list> : This helps in wiring ie 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.

Explain Spring Bean Autowiring?

  • In spring framework, setting bean dependencies in configuration files is a good practice to follow, but the spring container is also able to autowire relationships between collaborating beans.
  • This means that it is possible to automatically let Spring resolve collaborators (other beans) for your bean by inspecting the contents of the BeanFactory.

Explain @Required annotation with example?

@Required annotation is used to check only the required attributes of bean properties configured in context files.

Spring’s dependency checking feature “dependency-check” attribute, will not help you in this case.

What are the types of events in spring framework?

  • ContextRefreshedEvent: It is raised when the ApplicationContext is either initialized or refreshed.
  • ContextStartedEvent : This event is raised when the ApplicationContext is started using the start() method on the ConfigurableApplicationContext interface.
  • ContextStoppedEvent : This event is published when the ApplicationContext is stopped using the stop() method on the ConfigurableApplicationContext interface.
  • ContextClosedEvent : This event is published when the ApplicationContext is closed using the close() method on the ConfigurableApplicationContext interface.
  • RequestHandledEvent : This is a web-specific event telling all beans that an HTTP request has been serviced.

What is XML BeanFactory?

  • The most useful one is org.springframework.beans.factory.xml.XmlBeanFactory, which loads its beans based on the definitions contained in an XML file.
  • This container reads the configuration metadata from an XML file and uses it to create a fully configured system or application.

What is AOP module?

  • The AOP module is used for developing aspects for our Spring-enabled application.
  • Much of the support has been provided by the AOP Alliance in order to ensure the interoperability between Spring and other AOP frameworks.
  • This module also introduces metadata programming to Spring.

Explain the JDBC abstraction and DAO module?

  • With the JDBC abstraction and DAO module we can be sure that we keep up the database code clean and simple, and prevent problems that result from a failure to close database resources.
  • It provides a layer of meaningful exceptions on top of the error messages given by several database servers.
  • It also makes use of Spring’s AOP module to provide transaction management services for objects in a Spring application.

Explain the object/relational mapping integration module

  • Spring also supports for using of an object/relational mapping (ORM) tool over straight JDBC by providing the ORM module.
  • Spring provides support to tie into several popular ORM frameworks, including Hibernate, JDO, and iBATIS SQL Maps.
  • Spring’s transaction management supports each of these ORM frameworks as well as JDBC.

Explain the web module

  • The Spring web module is built on the application context module, providing a context that is appropriate for web-based applications.
  • This module also contains support for several web-oriented tasks such as transparently handling multipart requests for file uploads and programmatic binding of request parameters to your business objects.
  • It also contains integration support with Jakarta Struts.

Explain the Spring MVC module

  • MVC framework is provided by Spring for building web applications.
  • Spring can easily be integrated with other MVC frameworks, but Spring’s MVC framework is a better choice, since it uses IoC to provide for a clean separation of controller logic from business objects.
  • With Spring MVC you can declaratively bind request parameters to your business objects.

Explain the Core Container (Application context) module

  • This is the basic Spring module, which provides the fundamental functionality of the Spring framework.
  • BeanFactory is the heart of any spring-based application. Spring framework was built on the top of this module, which makes the Spring container.

What are the Spring support ORM’s ?

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

What is AOP is spring?

  • 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 transaction management.

What is a Proxy?

  • A proxy is an object that is created by applying advice to a target object.
  • When you think of client objects the target object and the proxy object are the same.