Oracle Java Interview Questions & Answers Set-15

«« Previous
Next »»

Spring Interview Questions


281) What is Spring?
Ans: It is a lightweight, loosely coupled and integrated framework for developing enterprise applications in java.


282) What are the advantages of spring framework?
Ans: 
  • Predefined Templates
  • Loose Coupling
  • Easy to test
  • Lightweight
  • Fast Development
  • Powerful Abstraction
  • Declarative support

283) What are the modules of spring framework?
Ans: 
  • Test
  • Spring Core Container
  • AOP, Aspects and Instrumentation
  • Data Access/Integration
  • Web

284) What is IOC and DI?
Ans: IOC (Inversion of Control) and DI (Dependency Injection) is a design pattern to provide loose coupling. It removes the dependency from the program.

Let's write a code without following IOC and DI.

public class Employee{  
Address address;  
Employee(){  
address=new Address();//creating instance  
}  
}  

Now, there is dependency between Employee and Address because Employee is forced to use the same address instance.

Let's write the IOC or DI code.

public class Employee{  
Address address;  
Employee(Address address){  
this.address=address;//not creating instance  
}  
Now, there is no dependency between Employee and Address because Employee is not forced to use the same address instance. It can use any address instance.


285) What is the role of IOC container in spring?
Ans: IOC container is responsible to:
  • create the instance
  • configure the instance, and
  • assemble the dependencies

286) What are the types of IOC container in spring?
Ans: There are two types of IOC containers in spring framework.
  1. BeanFactory
  2. ApplicationContext

287) What is the difference between BeanFactory and ApplicationContext?
Ans: BeanFactory is the basic container whereas ApplicationContext is the advanced container. ApplicationContext extends the BeanFactory interface. ApplicationContext provides more facilities than BeanFactory such as integration with spring AOP, message resource handling for i18n etc.


288) What is the difference between constructor injection and setter injection?
Ans: 
Constructor Injection
Setter Injection
No Partial Injection Partial Injection
Desn't override the setter property Overrides the constructor property if both are defined.
Creates new instance if any modification occurs Doesn't create new instance if you change the property value
Better for too many properties Better for few properties.

289) What is autowiring in spring? What are the autowiring modes?
Ans: Autowiring enables the programmer to inject the bean automatically. We don't need to write explicit injection logic.

Let's see the code to inject bean using dependency injection.

<bean id="emp" class="com.javatpoint.Employee" autowire="byName" />  

The autowiring modes are given below:

Mode
Description
No this is the default mode, it means autowiring is not enabled.
byName injects the bean based on the property name. It uses setter method.
byType injects the bean based on the property type. It uses setter method.
constructor It injects the bean using constructor


290) What are the different bean scopes in spring?
Ans: There are 5 bean scopes in spring framework.

Scope
Description
singleton The bean instance will be only once and same instance will be returned by the IOC container. It is the default scope.
prototype The bean instance will be created each time when requested.
request The bean instance will be created per HTTP request.
session The bean instance will be created per HTTP session.
globalsession The bean instance will be created per HTTP global session. It can be used in portlet context only.

291) In which scenario, you will use singleton and prototype scope?
Ans: Singleton scope should be used with EJB stateless session bean and prototype scope with EJB stateful session bean.


292) What are the transaction management supports provided by spring?
Ans: Spring framework provides two type of transaction management supports:

  1. Programmatic Transaction Management: should be used for few transaction operations.
  2. Declarative Transaction Management: should be used for many transaction operations.

Spring JDBC Interview Questions


293) What are the advantages of JdbcTemplate in spring?
Ans: Less code: By using the JdbcTemplate class, you don't need to create connection,statement,start transaction,commit transaction and close connection to execute different queries. You can execute the query directly.


294) What are classes for spring JDBC API?
Ans: 
  1. JdbcTemplate
  2. SimpleJdbcTemplate
  3. NamedParameterJdbcTemplate
  4. SimpleJdbcInsert
  5. SimpleJdbcCall

295) How can you fetch records by spring JdbcTemplate?
Ans: You can fetch records from the database by the query method of JdbcTemplate. There are two interfaces to do this:
  1. ResultSetExtractor
  2. RowMapper

296) What is the advantage of NamedParameterJdbcTemplate?
Ans: NamedParameterJdbcTemplate class is used to pass value to the named parameter. A named parameter is better than ? (question mark of PreparedStatement).

It is better to remember.


297) What is the advantage of SimpleJdbcTemplate?
Ans: The SimpleJdbcTemplate supports the feature of var-args and autoboxing.


Spring AOP Interview Questions


298) What is AOP?
Ans: AOP is an acronym for Aspect Oriented Programming. It is a methodology that divides the program logic into pieces or parts or concerns.

It increases the modularity and the key unit is Aspect.


299) What are the advantages of spring AOP?
Ans: AOP enables you to dynamically add or remove concern before or after the business logic. It is pluggable and easy to maintain.

300) What are the AOP terminology?
Ans: AOP terminologies or concepts are as follows:
  • JoinPoint
  • Advice
  • Pointcut
  • Aspect
  • Introduction
  • Target Object
  • Interceptor
  • AOP Proxy
  • Weaving
«« Previous
Next »»