September 24, 2018

Srikaanth

EMC Spring MVC Recently Asked Interview Questions Answers

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" />
EMC Spring MVC Recently Asked Interview Questions Answers
EMC Spring MVC Recently Asked Interview Questions Answers
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.

What are the different types of Advice?

There are different types of Advice:

Before Advice :

The advice which executed before a join point called before advice. The before advice does not have the ability to prevent the execution flow proceeding at the join point (unless it throws an exception).

After Return Advice :

The advice which executed after a join point completed normally without any exception.

Around Advice :

It is responsible for choosing whether to proceeds to the join point or shortcut the advised method execution by returning its own return value or throwing an exception. This is most powerful kind of advice. With Around advice you can perform custom behavior before and after method execution.

After Throwing Advice :

The advice executed when a method throws an exception.

After (finally) Advice :

The advice is executed when program exits the join points either normally or by throwing an exception.

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 Spring MVC Interceptor and how to use it?

Spring MVC Interceptors are like Servlet Filters and allow us to intercept client request and process it. We can intercept client request at three places – preHandle, postHandle and afterCompletion.

We can create spring interceptor by implementing HandlerInterceptor interface or by extending abstract class HandlerInterceptorAdapter.

We need to configure interceptors in the spring bean configuration file. We can define an interceptor to intercept all the client requests or we can configure it for specific URI mapping too.

What is Spring JdbcTemplate class and how to use it?

Spring Framework provides excellent integration with JDBC API and provides JdbcTemplate utility class that we can use to avoid bolier-plate code from our database operations logic such as Opening/Closing Connection, ResultSet, PreparedStatement etc.


Subscribe to get more Posts :