Spring Overview Interview Questions Part II

What is the Spring DAO support

The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies like JDBC, Hibernate or JDO in a consistent way. This allows us to switch between the persistence technologies fairly easily and to code without worrying about catching exceptions that are specific to each technology.

Which ORM’s does Spring support?

Spring supports the following ORM’s:

  • Hibernate

  • iBatis

  • JPA (Java Persistence API)

  • TopLink

  • JDO (Java Data Objects)

  • OJB

Learn Spring from our Experts

What are the ways to access Hibernate by using Spring?

There are two ways to access Hibernate with Spring:

  • Inversion of Control with a Hibernate Template and Callback.

  • Extending HibernateDAOSupport and Applying an AOP Interceptor node.

How can we integrate Spring and Hibernate using HibernateDaoSupport?

Use Spring’s SessionFactory called LocalSessionFactory. The integration process is of 3 steps:

  • Configure the Hibernate SessionFactory.

  • Extend a DAO Implementation from HibernateDaoSupport.

  • Wire in Transaction Support with AOP.

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.

Types of the transaction management Spring support.

Spring supports two types of transaction management:

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

  • Declarative transaction management: This means you separate transaction management from the business code. You only use annotations or XML based configuration to manage the transactions.

What are the benefits of the Spring Framework’s transaction management?
  • It provides a consistent programming model across different transaction APIs such as JTA, JDBC, Hibernate, JPA, and JDO.

  • It provides a simpler API for programmatic transaction management than a number of complex transaction APIs such as JTA.

  • It supports declarative transaction management.

  • It integrates very well with Spring’s various data access abstractions.

Explain AOP.

Aspect-oriented programming, or AOP, is a programming technique that allows programmers to modularize cross-cutting concerns, or behavior that cuts across the typical divisions of responsibility, such as logging and transaction management.

What is the Aspect?
  • The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules. It ia a module which has a set of APIs providing cross-cutting requirements. For example, a logging module would be called AOP aspect for logging.

  • An application can have any number of aspects depending on the requirement. In Spring AOP, aspects are implemented using regular classes annotated with the @Aspect annotation (@AspectJ style).

Which Transaction management type is more preferable?
  • Most users of the Spring Framework choose declarative transaction management because it is the option with the least impact on application code, and hence is most consistent with the ideals of a non-invasive lightweight container.

  • Declarative transaction management is preferable over programmatic transaction management though it is less flexible than programmatic transaction management, which allows you to control transactions through your code.

What is the Join point?

The join point represents a point in an application where we can plug-in an AOP aspect. It is the actual place in the application where an action will be taken using Spring AOP framework.

What is the difference between concern and cross-cutting concern in Spring AOP?
  • The Concern is behavior we want to have in a module of an application. A Concern may be defined as a functionality we want to implement.

  • The cross-cutting concern is a concern which is applicable throughout the application and it affects the entire application. For example, logging, security and data transfer are the concerns which are needed in almost every module of an application, hence they are cross-cutting concerns.

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 is Introduction?

An Introduction allows us to add new methods or attributes to existing classes.

What is a Proxy?

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

Explain Pointcut.

The pointcut is a set of one or more joinpoints where an advice should be executed. You can specify pointcuts using expressions or patterns.

What is Weaving? What are the different points where weaving can be applied?

Weaving is the process of linking aspects with other application types or objects to create an advised object. Weaving can be done at compile time, at load time, or at runtime.

What are the different types of AutoProxying?
  • BeanNameAutoProxyCreator

  • DefaultAdvisorAutoProxyCreator

  • Metadata autoproxying

Explain XML Schema-based aspect implementation?

In this implementation case, aspects are implemented using regular classes along with XML based configuration.

Explain annotation-based (@AspectJ based) aspect implementation.

This implementation case (@AspectJ based implementation) refers to a style of declaring aspects as regular Java classes annotated with Java 5 annotations.

Define DispatcherServlet.

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

What are the different types of IoC (dependency injection)?
  • Constructor-based dependency injection: Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on other class.

  • Setter-based dependency injection: Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.

What are inner beans in Spring?

When a bean is only used as a property of another bean it can be declared as an inner bean. Spring’s XML-based configuration metadata provides the use of element inside the or elements of a bean definition, in order to define the so-called inner bean. Inner beans are always anonymous and they are always scoped as prototypes.

What is Spring MVC framework?

Spring comes with a full-featured MVC framework for building web applications. Although Spring can easily be integrated with other MVC frameworks, such as Struts, Spring’s MVC framework uses IoC to provide a clean separation of controller logic from business objects. It also allows to declaratively bind request parameters to business objects.

What is Dependency Injection in Spring?

Dependency Injection, an aspect of Inversion of Control (IoC), is a general concept, and it can be expressed in many different ways.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.

Explain the Core Container (Application context) module
  • Application contexts provide a means for resolving text messages, a generic way to load file resources (such as images), they can publish events to beans that are registered as listeners.

  • In addition, operations on the container or beans in the container, which have to be handled in a programmatic fashion with a bean factory, can be handled declaratively in an application context.

  • The application context implements MessageSource, an interface used to obtain localized messages, with the actual implementation being pluggable.

What does a Spring application look like?
  • An interface that defines the functions.

  • The implementation that contains properties, its setter and getter methods, functions etc.

  • Spring AOP.

  • The Spring configuration XML file.

  • Client program that uses the function.

What are the benefits of IOC?

IOC or dependency injection minimizes the amount of code in an application. It makes easy to test applications, since no singletons or JNDI lookup mechanisms are required in unit tests. Loose coupling is promoted with minimal effort and least intrusive mechanism. IOC containers support eager instantiation and lazy loading of services.

Which are the important beans lifecycle methods? Can you override them?
  • There are two important bean lifecycle methods. The first one is setup which is called when the bean is loaded in to the container. The second method is the teardown method which is called when the bean is unloaded from the container.

  • The bean tag has two important attributes (init-method and destroy-method) with which you can define your own custom initialization and destroy methods.

  • There are also the correspondive annotations(@PostConstruct and @PreDestroy).

How do you define the scope of a bean?
  • When defining a in Spring, we can also declare a scope for the bean. It can be defined through the scope attribute in the bean definition. For example, when Spring has to produce a new bean instance each time one is needed, the bean’s scope attribute to be prototype.

  • On the other hand, when the same instance of a bean must be returned by Spring every time it is needed, the the bean scope attribute must be set to singleton.

What are the common implementations of the ApplicationContext?
  • The FileSystemXmlApplicationContext container loads the definitions of the beans from an XML file. The full path of the XML bean configuration file must be provided to the constructor.

  • The ClassPathXmlApplicationContext container also loads the definitions of the beans from an XML file. Here, you need to set CLASSPATH properly because this container will look bean configuration XML file in CLASSPATH.

  • The WebXmlApplicationContext: container loads the XML file with definitions of all beans from within a web application.

Explain Bean lifecycle in Spring framework.

The spring container finds the bean’s definition from the XML file and instantiates the bean.

  • Spring populates all of the properties as specified in the bean definition (DI).

  • If the bean implements BeanNameAware interface, spring passes the bean’s id to setBeanName() method.

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

  • If there are any bean BeanPostProcessors associated with the bean, Spring calls postProcesserBeforeInitialization() method.

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

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

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

Are Singleton beans thread safe in Spring Framework?

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

Explain the bean scopes supported by Spring

There are five scoped provided by the Spring Framework supports following five scopes:

  • In singleton scope, Spring scopes the bean definition to a single instance per Spring IoC container.

  • In prototype scope, a single bean definition has any number of object instances.

  • In request scope, a bean is defined to an HTTP request. This scope is valid only in a web-aware Spring ApplicationContext.

  • In session scope, a bean definition is scoped to an HTTP session. This scope is also valid only in a web-aware Spring ApplicationContext.

  • In global-session scope, a bean definition is scoped to a global HTTP session. This is also a case used in a web-aware Spring ApplicationContext.

The default scope of a Spring Bean is Singleton.

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.

What is Controller in Spring MVC framework?

Controllers provide access to the application behavior that you typically define through a service interface. Controllers interpret user input and transform it into a model that is represented to the user by the view. Spring implements a controller in a very abstract way, which enables you to create a wide variety of controllers.

What is @RequestMapping annotation?

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

What is @Controller annotation?

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.

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.

What is Spring IoC container?

The Spring IoC is responsible for creating the objects,managing them (with dependency injection (DI)), wiring them together, configuring them, as also managing their complete lifecycle.

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

Learn more about Spring in our Video Tutorial