Top 50 Java & Ejb & Spring Interview Questions You Must Prepare 19.Mar.2024

A message driven bean is a type of enterprise bean which is invoked by EJB container when it receives a message from queue or topic. Message driven bean is a stateless bean and is used to do task asynchronously.

 

Following are the key benefits of EJB.

  • Simplified development of large scale enterprise level application.
  • Application Server/ EJB container provides most of the system level services like traction handling, logging, load balancing, persistence mechanism, exception handling and so on. Developer has to focus only on business logic of the application.
  • EJB container manages life cycle of ejb instances thus developer needs not to worry about when to create/delete ejb objects.

 

Class level interceptor is invoked for every method of the bean. Class level interceptor can be applied both by annotation or via xml(ejb-jar.xml).

Session bean stores data of a particular user for a single session. It can be stateful or stateless. It is less resource intensive as compared to entity be. Session bean gets destroyed as soon as user session terminates.

It refers to the ability to make a class abstract in OOP. It helps to reduce the complexity and also improves the maintainability of the system.

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.

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.

  • The FileSystemXmlApplicationContext container loads the definitions of the be 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 be 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 be from within a web application.

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 traction management services for objects in a Spring application.

Checked exceptions can be caught at the time of program compilation. Checked exceptions must be handled by using try catch block in the code in order to successfully compile the code.

main method is an entry point of Java class and is required for execution of the program however; a class gets compiled successfully even if it doesn’t have a main method. It can’t be run though.

In Java, if a variable is used in a code without prior initialization by a valid value, program doesn’t compile and gives an error as no default value is assigned to variables in Java.

If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.

A stateless session bean is a type of enterprise bean which is normally used to do independent operations. A stateless session bean as per its name does not have any associated client state, but it may preserve its instance state. EJB Container normally creates a pool of few stateless bean's objects and use these objects to process client's request. Because of pool, instance variable values are not guaranteed to be same across lookups/method calls.

An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

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.

A stateful session bean is a type of enterprise bean which preserve the conversational state with client. A stateful session bean as per its name keeps associated client state in its instance variables. EJB Container creates a separate stateful session bean to process client's each request. As soon as request scope is over, statelful session bean is destroyed.

JDBC can be used more efficiently with the help of a template class provided by spring framework called as JdbcTemplate.

With use of Spring JDBC framework the burden of resource management and error handling is reduced a lot. So it leaves developers to write the statements and queries to get the data to and from the database. 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.

A thread in Java can be in either of the following states:
Ready: When a thread is created, it’s in Ready state.
Running: A thread currently being executed is in running state.
Waiting: A thread waiting for another thread to free certain resources is in waiting state.
Dead: A thread which has gone dead after execution is in dead state.

A traction is a single unit of work items which follows the ACID properties. ACID stands for Atomic, Consistent,Isolated and Durable.

@javax.ejb.MessageDrivenBean annotation specifies that a given ejb class is a message driven bean. Following are its attributes:
name - Used to specify name of the message driven bean.
messageListenerInterface - Used to specify message listener interface for the message driven bean.
activationConfig - Used to specify the configuration details of the message-driven bean in operational environment of the message driven bean.
mappedName - Used to specify the JNDI name of the message driven bean.
description - Used to provide description of the message driven bean.

Entity be represents persistent data storage. User data can be saved to database via entity be and later on can be retrived from the database in the entity bean.

In java, a thread which is in dead state can’t be started again. There is no way to restart a dead thread.

If a method does not handle a checked exception, the method must declare it using the throwskeyword. The throws keyword appears at the end of a method's signature..

Primitive data types like int can be handled as objects by the use of their respective wrapper classes. For example, Integer is a wrapper class for primitive data type int. We can apply different methods to a wrapper class, just like any other object.

If the method overrides one of its superclass's methods, overridden method can be invoked through the use of the keyword super. It can be also used to refer to a hidden field.

@javax.ejb.Local annotation is used to specify Local interface(s) of a session bean. This local interface states the business methods of the session bean (which can be stateless or stateful).
This interface is used to expose the business methods to local clients which are running in same deployment/application as EJB.
Following are its attributes:
value - Used to specify the list of local interfaces as an array of interfaces.

@javax.ejb.Stateless annotation specifies that a given ejb class is a stateless session bean.Following are its attributes:
name - Used to specify name of the session bean.
mappedName - Used to specify the JNDI name of the session bean.
description - Used to provide description of the session bean.

Following are the differences between stateful session bean and stateless session bean:
EJB Container creates a separate stateful session bean to process client's each request.whereas EJB Container normally creates a pool of few stateless bean's objects and use these objects to process client's request.
As soon as request scope is over, statelful session bean is destroyed but stateless bean remains active.
A stateful session bean is a type of enterprise bean which preserve the conversational state with client. A stateful session bean as per its name keeps associated client state in its instance variables Whereas because of pool of stateless session be, instance variable values are not guaranteed to be same across lookups/method calls in stateless session be.

In Java, a class can be derived from only one class and not from multiple classes. Multiple inheritances is not supported by Java.

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 traction management supports each of these ORM frameworks as well as JDBC.

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.

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
  • Traction module
  • Web module
  • Web-Servlet module
  • Web-Struts module
  • Web-Portlet module

Following are the key components of persistence API in EJB:
Entity - A persistent object representing the data-store record. It is good to be serializable.
EntityManager - Persistence interface to do data operations like add/delete/update/find on persistent object(entity). It also helps to execute queries using Query interface.
Persistence unit (persistence.xml) - Persistence unit describes the properties of persistence mechanism.
Data Source (*ds.xml) - Data Source describes the data-store related properties like connection url. user-name,password etc.

Default interceptor is invoked for every bean within deployment.Default interceptor can be applied only via xml (ejb-jar.xml).

Spring aspects can work with five kinds of advice mentioned below:
before: Run advice before the a method execution.
after: Run advice after the a method execution regardless of its outcome.
after-returning: Run advice after the a method execution only if method completes successfully.
after-throwing: Run advice after the a method execution only if method exits by throwing an exception.
around: Run advice before and after the advised method is invoked.

 

if ejb client is in different environment where ejb session bean is to be deployed then we use remote session bean.

The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.

Method level interceptor is invoked for a particular method of the bean. Method level interceptor can be applied both by annotation of via xml(ejb-jar.xml).

These classes cannot be instantiated and are either partially implemented or not at all implemented. This class contains one or more abstract methods which are simply method declarations without a body.

EJB 3.0 provides option to embed JAVA POJO (Plain Old Java Object) into an entity bean and allows to map column names with the methods of the embedded POJO class. A java POJO to be embedded must be annotated as @Embeddable.

Non-Static methods are owned by objects of a class and have object level scope and in order to call the non-Static methods from a static block (like from a static main method), an object of the class needs to be created first. Then using object reference, these methods can be invoked.

 

Message driven bean is a stateless bean and is used to do task asynchronously.

The Exception class has two main subclasses : IOException class and RuntimeException Class

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.

In Java, there is not goto keyword and java doesn’t support this feature of going to a particular labeled line.

 

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

An exception can be thrown, either a newly instantiated one or an exception that you just caught, by using throw keyword.

Constructor in Java must have same name as the class name and if the name is different, it doesn’t act as a constructor and compiler thinks of it as a normal method.