RELX Group Frequently Asked Spring MVC Interview Questions

What is the difference between singleton and prototype bean?

Mainly it is the scope of a beans which defines their existence on the application
Singleton : It means single bean definition to a single object instance per Spring IOC container.
Prototype : It means a single bean definition to any number of object instances.

How do beans become 'singleton' or prototype?

- There exists an attribute in bean tag, called 'singleton’.
- If it is marked 'true', the bean becomes 'singleton'.
- If it is marked 'false', the bean becomes 'prototype'.
What type of transaction Management Spring support?

Spring supports two types of transaction management:

1. Programmatic transaction management
2. Declarative transaction management.

When do you use programmatic and declarative transaction management?

Programmatic transaction management is used preferably when you have a small number of transactional operations.

Incase of large number of transactional operations it is better to use declarative transaction management.

What is IOC?

- IOC stands for Inversion of Control pattern.
- It is also called as dependency injection.
- This concept says that you do not create your objects but describe how they should be created.
- Similarly, you do not directly connect your components and services together in code but describe which services are needed by which components in a configuration file.
- A container then hooks them all up.
RELX Group Frequently Asked Spring MVC Interview Questions Answers
RELX Group Frequently Asked Spring MVC Interview Questions Answers

What are the different types of IoC (dependency injection)?

- There are three types of dependency injection:
1. Constructor Injection : Here dependencies are provided as constructor parameters.
2. Setter Injection : Dependencies are assigned through JavaBeans properties.
3. Interface Injection : Injection is performed through an interface.
- Spring supports only first two categories of Injection.

What are the benefits of IOC?

The main benefits of IOC or dependency injections are :
1. It minimizes the amount of code in your application.
2. It makes your application easy to test as it doesn't require any singletons or JNDI lookup mechanisms in your unit test cases.
3. Loose coupling is promoted with minimal effort and least intrusive mechanism.
4. IOC containers support eager instantiation and lazy loading of services.

What is Bean Wiring?

- Bean wiring means creating associations between application components i.e. beans within the spring container.

How do you access Hibernate using Spring ?

There are two ways to Spring’s Hibernate integration:
1. By Inversion of Control with a HibernateTemplate and Callback.
2. By extending HibernateDaoSupport and Applying an AOP Interceptor.

How would you integrate Spring and Hibernate using HibernateDaoSupport?

This can be done through Spring’s SessionFactory called LocalSessionFactory. The steps in integration process are:

1. Configure the Hibernate SessionFactory.
2. Extend your DAO Implementation from HibernateDaoSupport.
3. Wire in Transaction Support with AOP.

What are the various transaction manager implementations in Spring?

1. DataSourceTransactionManager : PlatformTransactionManager implementation for single JDBC data sources.

2. HibernateTransactionManager: PlatformTransactionManager implementation for single Hibernate session factories.

3. JdoTransactionManager : PlatformTransactionManager implementation for single JDO persistence manager factories.

4. JtaTransactionManager : PlatformTransactionManager implementation for JTA, i.e. J2EE container transactions.

What are the different modules in spring framework?

The Spring features or organized into about 20 modules. These modules are grouped into Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation and Test, as depicted below.

What is Auto Wiring in Spring?

- The Auto-wiring in spring framework can be performed by configuring in xml and Spring Auto-Wiring with Annotation @Autowired.

- Auto-wiring beans with xml configuration: In Spring framework, you can wire beans automatically with auto-wiring feature. To enable auto-wiring just define the “autowire” attribute in <bean> tag.

<bean id="customer" class="com.test.autowire.Customer" autowire="byName" />

There are five modes of Auto-wiring supported.

1. no – Default, no auto wiring, set it manually via “ref” attribute.
<bean id="customer" class="com.test.autowire.Customer">
<property name="person" ref="person" />
</bean>
<bean id="person" class="com.test.autowire.Person" />

2. byName – Auto wiring by property name. If the name of a bean is same as the name of other bean property, auto wire it.

- In below example the name of the person bean is same as name of “customer” bean’s property Person object. So spring will auto-wire it via setter method.
<bean id="customer" class="com.test.autowire.Customer" autowire="byName"/>
<bean id="person" class="com.test.autowire.Person" />

3. byType – Auto wiring by property data type. If data type of a bean is compatible with the data type of other bean property, auto wire it.

- In below example the data type of the person bean is same as name of “customer” bean’s property Person object. So spring will auto-wire it via setter method.
<bean id="customer" class="com.test.autowire.Customer" autowire="byType"/>
<bean id="person" class="com.test.autowire.Person" />

4. constructor – byType mode in constructor argument.

- Here the data type of “person” bean is same as the constructor argument data type in “customer” bean’s property (Person object), so, Spring auto wired it via constructor method – “public Customer(Person person)”
<bean id="customer" class="com.test.autowire.Customer" autowire="constructor"/>
<bean id="person" class="com.test.autowire.Person" />

5. autodetect – If a default constructor is found, use “autowired by constructor”; Otherwise, use “autowire by type”.

- If a default constructor is found, uses “constructor”; Otherwise, uses “byType”. In this case, since there is a default constructor in “Customer” class, so, Spring auto wired it via constructor method – “public Customer(Person person)”.

<bean id="customer" class="com.test.autowire.Customer" autowire="autodetect"/>
<bean id="person" class="com.test.autowire.Person" />

What is JdbcTemplate in Spring? And how to use it?

The JdbcTemplate class is the main class of the JDBC Core package. The JdbcTemplate (The class internally use JDBC API) helps to eliminate lot of code you write with simple JDBC API (Creating connection, closing connection, releasing resources, handling JDB Exceptions, handle transaction etc.). The JdbcTemplate handles the creation and release of resources, which helps you to avoid common error like forgetting to close connection.

Examples :

1. Getting row count from database.

int rowCount = this.jdbcTemplate.queryForObject("select count(*) from t_employee", int.class);

2. Querying for a String.

String lastName = this.jdbcTemplate.queryForObject( "select last_name from t_employee where Emp_Id = ?", new Object[]{377604L}, String.class);

3. Querying for Object

Employee employee = this.jdbcTemplate.queryForObject( "select first_name, last_name from t_employee where Emp_Id = ?", new Object[]{3778604L}, new RowMapper()
{
public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
Employee employee = new Employee();
employee.setFirstName(rs.getString("first_name"));
employee.setLastName(rs.getString("last_name"));
return employee;
}
});

4. Querying for N number of objects.

List<Employee> employeeList = this.jdbcTemplate.query("select first_name, last_name from t_employee", new RowMapper<Employee>() {
public Employee mapRow(ResultSet rs, int rowNum) throws SQLException
{
   Employee employee = new Employee();
   employee.setFirstName(rs.getString("first_name"));
   employee.setLastName(rs.getString("last_name"));
   return employee;
}
});

What NamedParameterJdbcTemplate in Spring?

The NamedParameterJdbcTemplate allow basic set of JDBC operations, it allows named parameter in the query instead of traditional (?) placeholder, the functionality is similar to JdbcTemplate class.

Example :

NamedParameterJdbcTemplate namedParameterJdbcTemplate;

String empInsrtQuery = "INSERT INTO Employee (name, age, salary) VALUES (:name, :age, :salary)";
Map namedParameters = new HashMap();
namedParameters.put("name", name);
namedParameters.put("age", age);
namedParameters.put("salary", salary);
namedParameterJdbcTemplate.update(empInsrtQuery, namedParameters);

What are Advice, Aspect, Join-point and point cut in spring?

Advice :

An advice is an action taken by the aspect at particular join-point is called Advice.

Aspect :

An aspect is a subprogram which is associated with specific property of a program (Example separating logging code from the main program). An aspect is functionality or feature that cross cuts over object. AOP increase modularity of a program.

Join-Point :

A join point is a point used in spring AOP framework to represent a method execution. It always point during execution of program, method or exception. A join point is basically an opportunity within the code to apply aspect.

Point Cut :

In AOP a point cut is a set of many join points where an advice can execute. A chunk of code (known as Advice) associated with join point get executed.

Post a Comment

Previous Post Next Post