Spring Overview Interview Questions Part I

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 JavaEE platform.

  • Spring framework targets to make JavaEE development easier to use and promote good programming practice by enabling a POJO-based programming model.

Learn more about Spring in our Video Tutorial

What is BeanFactory?
  • A BeanFactory is an implementation of the factory pattern that applies Inversion of Control to separate the application’s configuration and dependencies from the actual application code.

  • The most commonly used BeanFactory implementation is the XmlBeanFactory class.

Explain the 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 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.

What are benefits of Spring Framework?
  1. Lightweight :
    Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 2MB.

  2. Inversion of Control (IOC) :
    Loose coupling is achieved in Spring, with the Inversion of Control technique. The objects give their dependencies instead of creating or looking for dependent objects.

  3. Aspect Oriented Programming (AOP) :
    Spring supports Aspect Oriented Programming and separates application business logic from system services.

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

  5. MVC Framework :
    Spring’s web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks.

  6. Transaction Management :
    Spring provides a consistent transaction management interface that can scale down to a local transaction and scale up to global transactions (JTA).

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

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 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.

Which DI would you suggest Constructor-based or Setter-based DI?

You can use both Constructor-based and Setter-based Dependency Injection. The best solution is using constructor arguments for mandatory dependencies and setters for optional dependencies.

What is XMLBeanFactory?

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.

Which are the Spring framework modules?

The basic modules of the Spring framework are :

  • 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

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.

Learn Spring from our Experts

What are Spring Beans?
  • The Spring Beans are Java Objects that form the backbone of a Spring application. They are instantiated, assembled, and managed by the Spring IoC container. These beans are created with the configuration metadata that is supplied to the container, for example, in the form of XML definitions.

  • Beans defined in spring framework are singleton beans. There is an attribute in bean tag named "singleton" if specified true then bean becomes singleton and if set to false then the bean becomes a prototype bean. By default it is set to true. So, all the beans in spring framework are by default singleton beans.

What does a Spring Bean definition contain?

A Spring Bean definition contains all configuration metadata which is needed for the container to know how to create a bean, its lifecycle details and its dependencies.

How do you provide configuration metadata to the Spring Container?

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

  1. XML based configuration file.

  2. Annotation-based configuration.

  3. Java-based configuration.

How can you inject a Java Collection in Spring?

Spring offers the following types of collection configuration elements :

  • The type is used for injecting a list of values, in the case that duplicates are allowed.

  • The type is used for wiring a set of values but without any duplicates.

  • The type is used to inject a collection of name-value pairs where name and value can be of any type.

  • The type can be used to inject a collection of name-value pairs where the name and value are both Strings.

What is Bean Auto Wiring?

The Spring container is able to autowire relationships between collaborating beans. This means that it is possible to automatically let Spring resolve collaborators (other beans) for a bean by inspecting the contents of the BeanFactory without using and elements.

Explain 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. Explicit bean reference should be used for wiring.

  • byName :
    When autowiring byName, the Spring container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file.

  • byType :
    When autowiring by datatype, the Spring container looks at the properties of the beans 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 beans name in configuration file. If more than one such beans exist, a fatal exception is thrown.

  • constructor :
    This mode is 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.

What is Bean wiring?

Wiring, or else bean wiring is the case when beans are combined together within the Spring container. When wiring beans, the Spring container needs to know what beans are needed and how the container should use dependency injection to tie them together.

Are there limitations with autowiring?

Limitations of autowiring are :

  • Overriding :
    You can still specify dependencies using and settings which will always override autowiring.

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

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

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.

An example is the @Configuration annotation, that indicates that the class can be used by the Spring IoC container as a source of bean definitions. Another example is the@Bean annotated method that will return an object that should be registered as a bean in the Spring application context.

How do you turn on annotation wiring?

Annotation wiring is not turned on in the Spring container by default. In order to use annotation based wiring we must enable it in our Spring configuration file by configuring element.

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.

What is the meaning of @Required annotation

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.

Explain @Autowired annotation.

The @Autowired annotation provides more fine-grained control over where and how autowiring should be accomplished. It can be used to autowire bean on the setter method just like @Required annotation, on the constructor, on a property or pn methods with arbitrary names and/or multiple arguments.

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.

Explain @Qualifier annotation.

When there are more than one beans of the same type and only one is needed to be wired with a property, the @Qualifier annotation is used along with @Autowired annotation to remove the confusion by specifying which exact bean will be wired.

What is JdbcTemplate?

JdbcTemplate class 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.

How can JDBC be used more efficiently in the Spring framework?
  • When using the Spring JDBC framework the burden of resource management and error handling is reduced. So developers only need to write the statements and queries to get the data to and from the database.

  • JDBC can be used more efficiently with the help of a template class provided by Spring framework, which is the JdbcTemplate (example here).