Interview Questions

Spring

Spring Quartz Scheduler

In this article lets discuss about using quartz scheduler in spring application.

Quartz is an API used to schedule jobs in Java application. Consider a scenario, where it is required to execute a particular activity at 10 AM everyday. We can have a java method to call that particular functionality and configure quartz api to schedule the job. We can specify the schedule using a Cron expression.

What is a Cron Expression?

A Cron-Expression is a string consisting of 6 to 7 fields separated by white space.

Spring DWR Integration

In this section, let us discuss about DWR integration with Spring.

DWR (DIRECT WEB REMOTING) is a framework to use Ajax in Java web application.

Lets consider an example of populating child drop down list from parent using DWR in Spring application. Consider the following register.jsp file

register.jsp

<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

Spring Mail Sender

In this article, lets discuss about Sending email using Spring framework support.

Sending an email using Spring framework is made simple using org.springframework.mail.javamail.JavaMailSender and org.springframework.mail.javamail.MimeMessageHelper beans in Spring.

Consider the SpringMailSender class below.

SpringMailSender Class

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR

Spring Internationalization (I18N)

In this section, let us discuss about Internationalization (i18n) in Spring application.

In any web based application, all the text in the application UI (like names, labels), success/failure error message, validation messages etc will be in common English. In some scenarios, the Customer may require to display those text in regional specific languages.

Spring Refresh Application Context

In this section, we will discuss about how to reload an application context in Spring.

In many scenarios, you may require to reload the entire application context in Spring. But how to do it? There is actually an easy way to do. Have a look at the following code.

ApplicationContext ctx = new FileSystemXmlApplicationContext("Application-context.xml");
...
// Your application code here
...
((ConfigurableApplicationContext)ctx).refresh();

Spring Java RMI

In this section lets discuss about Java RMI (Remote Method Invocation) and how to implement the same using Spring.

Consider a scenario where you want to access a method which is available in a class in a different system or different JVM. This is usually called as Remote Procedure call.

Java provides a valuable API called RMI to make this work.

Following are the steps required to expose a RMI service in java.

Spring Web Service

In this section lets discuss about how to deploy a web service and access the same service with a client using spring framework.

Web Service – Introduction:

Spring AOP (Aspect Oriented Programming)

In this section lets discuss about Spring's AOP(Aspect Oriented Programming).

Spring - Load Custom File

Custom files can be loaded in a Spring application using Spring Resource API.

Create a class LoadFile with a Resource member variable as follows.

LoadFile Class

public class LoadFile
{
  private Resource  file;

  public LoadFile()
  {
  }

  public Resource getFile()
  {
    return file;
  }

  public void setFile(Resource file)
  {
    this.file = file;
  }
}

Now we need to inject any custom file to the LoadFile bean as follows

<bean id="loadFile" class="in.techdive.spring.examples.LoadFile">

Spring - Load Properties from file

In this section, we will discuss about loading Properties file in Spring.

To load any property file in Spring, add the following bean definition in Spring configuration file.

XML Configuration

<bean id="propertyConfigurator" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <description>
        Configurer that replaces ${...} placeholders with values from a properties file
    </description>
   
    <property name="locations">
        <list>
            <value>classpath:user.properties</value>