Gartner Spring MVC Interview Questions Answers

Gartner Most Frequently Asked Spring MVC Latest Interview Questions Answers

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.

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.
Gartner Most Frequently Asked Spring MVC Latest Interview Questions Answers
Gartner Most Frequently Asked Spring MVC Latest Interview Questions Answers

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;
}
});

Post a Comment

Previous Post Next Post