Interview Questions

Spring

Spring 3.0 Transaction Management

For transaction management with Spring 2.0 refer the article Spring AOP

With respect to Spring 3.0, Transaction management can be done in a much simpler way.

<tx:annotation-driven transaction-manager="transactionManager"/>
      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

            <description>
                  Transaction manager for a single JDBC DataSource
            </description>

Spring 3.0 read values from property files

In Spring 2.0, we need to configure the property key in Spring bean configuration files
to read values from property files. Since Spring 3.0 is mostly annotation based, it is very simple to read values from property files.

Consider the following property file

file.properties

dir.location=C:\\tdd\\

To read the above property in java class use the following code

class FileController
{
        @Value("${dir.location}")
        private String directoryPath;
}

Spring Security - Count the login attempts

In Spring based web applications, it may be required to block a user, if he tries to login with invalid password more than "n" times.

This can be achieved if the application is configured with Spring Security framework.

We need to create an application listener as follows to trace the successful and failure login attempts.

import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AbstractAuthenticationEvent;
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;

Spring - Search Results using Display Tag

Display Tags are used in web applications to display a list of records in UI with pagination. It just takes a collection as input to the tag and displays the items in the list, limiting the records as per the page size. Display tags can be used with any UI framework like spring, struts etc., One common problem faced by developers (using spring framework) while displaying search results in same page is that if the subsequent pages in the paginated list are accessed, the search criteria will be lost. The search criteria will not be stored in session in spring MVC.

Spring - Abstract View

Consider a scenario, where you want to display content of an xml file or provide an excel/pdf as a part of http response in a spring based web application. Spring framework, provides an abstract view for this purpose.

AbstractView is an abstract class in spring api, provided with methods which should be implemented by the subclass, so that it can be sent as View inside the ModelAndView object from a spring controller.

This class is a simple replacement for jsp. Instead of redirecting the output to a jsp, we are using a concrete class extending the AbstractView class.

Spring Jasper Reports Integration

What is Jasper Reports ?

Spring Tutorial

Spring is a light weight open source framework, whose sole purpose is to make J2EE technologies easier to use. Spring by itself does not provide a solution for a particular problem (except Spring MVC); in turn it makes an outer layered architecture to integrate any other framework with your application in an easily maintainable way.
Enterprise applications can be developed in Java using Spring framework.

Modules
Spring framework comprises of different modules which are grouped under the following hierarchy.

  • Web MVC

Spring - Dependency Injection

Spring’s Core basically follows the Dependency Injection (DI) principle.

Generally, each object is responsible for obtaining its own references to the objects it collaborates with its dependencies. This can lead to tightly coupled and hard-to-test code.

When applying Dependency Injection, objects are given their dependencies at creation time by some external entity that coordinates each object in the system. In other words, dependencies are injected into objects. So, DI means an inversion of responsibility with regard to how an object obtains references to collaborating objects.

Spring JNDI Datasource Configuration in JBOSS

In this article let's discuss about configuring a JNDI Datasource from JBOSS in a Spring application.

Let's first create the following oracle-ds.xml (assume the db is Oracle) XML file.

oracle-ds.xml

<datasources>

<local-tx-datasource>

    <jndi-name>jdbc/OracleDS</jndi-name>

    <use-java-context>false</use-java-context>

    <connection-url>jdbc:oracle:thin:@10.1.1.1:1521:orcl</connection-url>

    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>

    <min-pool-size>2</min-pool-size>

Spring JFreeChart Integration

In this article let's discuss about how to integrate JFreeChart API with Spring framework.

What is JFreeChart?

JFreeChart is an API used for creating and displaying charts such as bar, pie etc in java applications. It supports several output types like Swing components, image files (including PNG and JPEG), and vector graphics file formats (including PDF, EPS and SVG). JFreeChart is an open source distributed under the terms of (LGPL).

Lets consider an example to display a Bar and Pie chart using JFreeChart in Spring Application.