Top 50 Java-springs Interview Questions You Must Prepare 28.Mar.2024

Spring provides a custom JavaServer Faces VariableResolver implementation that extends the standard Java Server Faces managed be mechanism which lets you use JSF and Spring together. This variable resolver is called as DelegatingVariableResolver

A point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution.

The RowCallbackHandler interface extracts values from each row of a ResultSet.
• Has one method – processRow(ResultSet)
• Called for each row in ResultSet.
• Typically stateful.

There are two types of bean injections.
@By setter
@By constructor

When wiring be, if a bean element is embedded to a property tag directly, then that bean is said to the Inner Bean. The drawback of this bean is that it cannot be reused anywhere else.

AOP is used in the Spring Framework: To provide declarative enterprise services, especially as a replacement for EJB declarative services. The most important such service is declarative traction management, which builds on the Spring Framework's traction abstraction.To allow users to implement custom aspects, complementing their use of OOP with AOP.

Types of advice:
• Before advice: Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).
• After returning advice: Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception.
• After throwing advice: Advice to be executed if a method exits by throwing an exception.
• After (finally) advice: Advice to be executed regardless of the me by which a join point exits (normal or exceptional return).
• Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception

An aspect is the cross-cutting functionality that you are implementing. It is the aspect of your application you are modularizing. An example of an aspect is logging. Logging is something that is required throughout an application. However, because applications tend to be broken down into layers based on functionality, reusing a logging module through inheritance does not make sense. However, you can create a logging aspect and apply it throughout your application using AOP.

The basic concept of the Inversion of Control pattern (also known as dependency injection) is 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 (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up.
i.e., Applying IoC, objects are given their dependencies at creation time by some external entity that coordinates each object in the system. That is, dependencies are injected into objects. So, IoC me an inversion of responsibility with regard to how an object obtains references to collaborating objects.

Spring and Hibernate can integrate using Spring’s SessionFactory called LocalSessionFactory. The integration process is of 3 steps.
• Configure the Hibernate SessionFactory
• Extend your DAO Implementation from HibernateDaoSupport
• Wire in Traction Support with AOP

@The spring container finds the be definition from the XML file and instantiates the bean.
@Using the dependency injection, spring populates all of the properties as specified in the bean definition.
@If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the be ID.
@If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.
@If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization() methods will be called.
@If an init-method is specified for the bean, it will be called.
@Finally, if there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.

The act of creating associations between application components (be) within the Spring container is reffered to as Bean wiring.

An introduction allows the user to add new methods or attributes to an existing class. This can then be introduced to an existing class without having to change the structure of the class, but give them the new behavior and state.

These applications are like any Java application. They are made up of several classes, each performing a specific purpose within the application. But these classes are configured and introduced to each other through an XML file. This XML file describes how to configure the classes, known as the Spring configuration file.

There are three types of dependency injection:
• Constructor Injection (e.g. Pico container, Spring etc): Dependencies are provided as constructor parameters.
• Setter Injection (e.g. Spring): Dependencies are assigned through JavaBe properties (ex: setter methods).
• Interface Injection (e.g. Avalon): Injection is done through an interface.

Most users of the Spring Framework choose declarative traction 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.

Spring's JdbcTemplate is central class to interact with a database through JDBC. 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.
JdbcTemplate template = new JdbcTemplate(myDataSource);

This module is provides the fundamental functionality of the spring framework. In this module BeanFactory is the heart of any spring-based application. The entire framework was built on the top of this module. This module makes the Spring container.

Spring Framework supports:
• Programmatic traction management.
• Declarative traction management.

Benefits of IOC (Dependency Injection) are as follows:
• Minimizes the amount of code in your application. With IOC containers you do not care about how services are created and how you get references to the ones you need. You can also easily add additional services by adding a new constructor or a setter method with little or no extra configuration.
• Make your application more testable by not requiring any singletons or JNDI lookup mechanisms in your unit test cases. IOC containers make unit testing and switching implementations very easy by manually allowing you to inject your own objects into the object under test.
• Loose coupling is promoted with minimal effort and least intrusive mechanism. The factory design pattern is more intrusive because components or services need to be requested explicitly whereas in IOC the dependency is injected into requesting piece of code. Also some containers promote the design to interfaces not to implementations design concept by encouraging managed objects to implement a well-defined service interface of your own.
• IOC containers support eager instantiation and lazy loading of services. Containers also provide support for instantiation of managed objects, cyclical dependencies, life cycles management, and dependency resolution between managed objects etc.

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. Using Springs metadata support, we will be able to add annotations to our source code that instruct Spring on where and how to apply aspects.

BeanFactory has many implementations in Spring. But one of the most useful one is org .spring framework .be .factory .xml .XmlBeanFactory, which loads its be based on the definitions contained in an XML file. To create an XmlBeanFactory, pass a java.io.InputStream to the constructor. The Input Stream will provide the XML to the factory. For example, the following code snippet uses a java .io .FileInput Stream to provide a bean definition XML file to XmlBeanFactory.

AOP Alliance is an open-source project whose goal is to promote adoption of AOP and interoperability among different AOP implementations by defining a common set of interfaces and components.

Spring provides a custom JavaServer Faces VariableResolver implementation that extends the standard JavaServer Faces managed be mechanism. When asked to resolve a variable name, the following algorithm is performed:
• Does a bean with the specified name already exist in some scope (request, session, application) If so, return it
• Is there a standard JavaServer Faces managed bean definition for this variable name? If so, invoke it in the usual way, and return the bean that was created.
• Is there configuration information for this variable name in the Spring WebApplicationContext for this application? If so, use it to create and configure an instance, and return that instance to the caller.
• If there is no managed bean or Spring definition for this variable name, return null instead.
• BeanFactory also takes part in the life cycle of a bean, making calls to custom initialization and destruction methods. As a result of this algorithm, you can trparently use either JavaServer Faces or Spring facilities to create be on demand.

SQLExceptionTrlator, is an interface to be implemented by classes that can trlate between SQLExceptions and Spring's own data-access-strategy-agnostic org.springframework.dao.Data Access Exception.

A modularization of a concern that cuts across multiple objects. Traction management is a good example of a crosscutting concern in J2EE applications. In Spring AOP, aspects are implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (@AspectJ style).

Spring is an open source framework created to address the complexity of enterprise application development. One of the chief advantages of the Spring framework is its layered architecture, which allows you to be selective about which of its components you use while also providing a cohesive framework for J2EE application development.

Programmatic traction management is usually a good idea only if you have a small number of tractional operations.

On the other hand, if your application has numerous tractional operations, declarative traction management is usually worthwhile. It keeps traction management out of business logic, and is not difficult to configure.

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

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 one to switch between the persistence technologies fairly easily and it also allows one to code without worrying about catching exceptions that are specific to each technology.

Spring supports the following ORM’s :
• Hibernate
• iBatis
• JPA (Java Persistence API)
• TopLink
• JDO (Java Data Objects)
• OJB

SQLProvider:
• Has one method – getSql()
• Typically implemented by PreparedStatementCreator implementers.
• Useful for debugging.

Spring DAO classes throw exceptions which are subclasses of DataAccessException . Spring provides a convenient trlation from technology-specific exceptions like SQLException to its own exception class hierarchy with the DataAccessException as the root exception. These exceptions wrap the original exception.

PreparedStatementCreator:
• Is one of the most common used interfaces for writing data to database.
• Has one method –createPreparedStatement(Connection)
• Responsible for creating a PreparedStatement.
• Does not need to handle SQLExceptions.

The three commonly used implementation of 'Application Context' are
• ClassPathXmlApplicationContext : It Loads context definition from an XML file located in the classpath, treating context definitions as classpath resources. The application context is loaded from the application's classpath by using the code .
ApplicationContext context = new
ClassPathXmlApplicationContext("bean.xml");
• FileSystemXmlApplicationContext : It loads context definition from an XML file in the filesystem. The application context is loaded from the file system by using the code .
ApplicationContext context = new
FileSystemXmlApplicationContext("bean.xml");
• XmlWebApplicationContext : It loads context definition from an XML file contained within a web application.

Spring - JSF integration is useful when an event handler wishes to explicitly invoke the bean factory to create be on demand, such as a bean that encapsulates the business logic to be performed when a submit button is pressed.

Action taken by an aspect at a particular join point. Different types of advice include "around," "before" and "after" advice. Many AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors "around" the join point.

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.

A target is the class that is being advised. The class can be a third party class or your own class to which you want to add your own custom behavior. By using the concepts of AOP, the target class is free to center on its major concern, unaware to any advice that is being applied.

Spring - JSF integration is useful when an event handler wishes to explicitly invoke the bean factory to create be on demand, such as a bean that encapsulates the business logic to be performed when a submit button is pressed.

The Application context module makes spring a framework. This module extends the concept of BeanFactory, providing support for internationalization (I18N) messages, application lifecycle events, and validation. This module also supplies many enterprise services such JNDI access, EJB integration, remoting, and scheduling. It also provides support to other framework.

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 Spring Framework provides a consistent abstraction for traction management that delivers the following benefits:
• Provides a consistent programming model across different traction APIs such as JTA, JDBC, Hibernate, JPA, and JDO.
• Supports declarative traction management.
• Provides a simpler API for programmatic traction management than a number of complex traction APIs such as JTA.
• Integrates very well with Spring's various data access abstractions.

This 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 trparently handling multipart requests for file uploads and programmatic binding of request parameters to your business objects. It also contains integration support with Jakarta Struts.

The basic approach is similar: it is possible to specify traction behavior (or lack of it) down to individual method level. It is possible to make a setRollbackOnly() call within a traction context if necessary. The differences are:
• Unlike EJB CMT, which is tied to JTA, the Spring Framework's declarative traction management works in any environment. It can work with JDBC, JDO, Hibernate or other tractions under the covers, with configuration changes only.
• The Spring Framework enables declarative traction management to be applied to any class, not merely special classes such as EJBs.
• The Spring Framework offers declarative rollback rules: this is a feature with no EJB equivalent. Both programmatic and declarative support for rollback rules is provided.
• The Spring Framework gives you an opportunity to customize tractional behavior, using AOP. With EJB CMT, you have no way to influence the container's traction management other than setRollbackOnly().
• The Spring Framework does not support propagation of traction contexts across remote calls, as do high-end application servers.

Advice is the implementation of an aspect. It is something like telling your application of a new behavior. Generally, and advice is inserted into an application at joinpoints.

A pointcut is something that defines at what joinpoints an advice should be applied. Advices can be applied at any joinpoint that is supported by the AOP framework. These Pointcuts allow you to specify where theadvice can be applied.

The process of applying aspects to a target object to create a new proxy object is called as Weaving. The aspects are woven into the target object at the specified join points.

There are two approaches to Spring’s Hibernate integration:
• Inversion of Control with a HibernateTemplate and Callback
• Extending HibernateDaoSupport and Applying an AOP Interceptor

Using this module we can keep up the database code clean and simple, and prevent problems that result from a failure to close database resources. A new layer of meaningful exceptions on top of the error messages given by several database servers is bought in this module. In addition, this module uses Springs AOP module to provide traction management services for objects in a Spring application.