Mindtree Frequently Asked Spring MVC Interview Questions Answers
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.
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 Weaving in Spring?
Weaving is the process of linking aspect with other application types or object to create an advised object. This can be performed at compile time, runtime and load time. In spring framework weaving is performed at runtime.
What is AOP Proxy?
AOP proxy is an object to implement the aspect contracts (advice method executions and so on). The AOP proxy is object is created by the AOP framework. In spring framework AOP proxy is JDK dynamic proxy or CGLIB proxy.
What is front controller in Spring MVC?
The Front Controller is basically a type of Design pattern which are being implemented in different framework (e.g. Struts and Spring MVC etc.).
In Spring MVC DispatcherServlet act as a Front Controller for the framework and responsible for intercepting every request and then dispatches/forwards request to appropriate controller. Configure the DispatcherServlet in the web.xml file of web application and request which we want to be handled by DispatcherServlet should be mapped using URL mapping.
For example all the requests ending with *.do will be handled by the DispatcherServlet.
<web-app>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
Difference between FileSystemResource and ClassPathResource.
In FileSystemResource you need to give the configuration file (i.e. spring-config.xml) relative to your project or the absolute location of the file.
In ClassPathResource spring looks for the file in the ClassPath so configuration (i.e. spring-config.xml) file should be included in the classpath. If spring-config.xml is in classpath, you can simply give the name of the file.
For Example: If your configuration file is at src/main/java/com/test/loadresource then your
FileSystemResource would be:
FileSystemResource resource = new FileSystemResource("src/main/java/com/test/loadresource/spring-config.xml");
And ClassPathResource would be :
ClassPathResource resource = new ClassPathResource("com/test/loadresource /spring-config.xml");
What is inner Bean Definition?
A bean definition added inside the property or constructor-arg elements are called inner bean.
- Example :
<bean id="outerbean" class="...">
<!-- instead of using a reference to a target bean, simply define the target bean inline -->
<property name="targetbean">
<bean class="com.example.Person"> <!-- this is the inner bean -->
<property name="name" value="XYZ"/>
<property name="age" value="35"/>
</bean>
</property>
</bean>
Give examples of how spring platform can be used by an application developer.
The spring platform can be used by an application developer in the following way :
Java method can be made to execute in a database transaction without having to deal with transaction APIs.
Local Java method can be made a remote procedure without having to deal with remote APIs.
Local Java method can be made a management operation without having to deal with JMX APIs.
Local Java method can be made a message handler without having to deal with JMS APIs.
What are the various ways of using spring?
There are various ways and forms in which springs can be used.
They are listed as follows:
1. Full-fledged Spring web app.
2. Spring middle-tier provides help of a third-party web framework.
3. Remote usage scenario: allow the system to be used to remotely use the resources from the server. The remote usage can be done to grab the data from the server or for troubleshooting the environment.
4. EJB -- Wrapping existing POJOs.
Specify the locations where spring can publish its artifacts.
Spring generally publishes its artifacts to four different places:
1. Community download site http://spring.io/downloads/community.
2. Maven Central, which is considered the default repository that Maven queries, and does not require any special configuration to use.
3. The Enterprise Bundle Repository (EBR), which is considered to be run by SpringSource and also hosts all the libraries that integrate with Spring.
4. Public Maven repository hosted on Amazon S3 for the development of snapshots and milestone releases. The jar file names are given in the same form as Maven Central, which makes it a useful place to get development versions of Spring to use with other libraries Spring Framework deployed in Maven Central.
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.
- 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" />
Mindtree Frequently Asked Spring MVC 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 Weaving in Spring?
Weaving is the process of linking aspect with other application types or object to create an advised object. This can be performed at compile time, runtime and load time. In spring framework weaving is performed at runtime.
What is AOP Proxy?
AOP proxy is an object to implement the aspect contracts (advice method executions and so on). The AOP proxy is object is created by the AOP framework. In spring framework AOP proxy is JDK dynamic proxy or CGLIB proxy.
What is front controller in Spring MVC?
The Front Controller is basically a type of Design pattern which are being implemented in different framework (e.g. Struts and Spring MVC etc.).
In Spring MVC DispatcherServlet act as a Front Controller for the framework and responsible for intercepting every request and then dispatches/forwards request to appropriate controller. Configure the DispatcherServlet in the web.xml file of web application and request which we want to be handled by DispatcherServlet should be mapped using URL mapping.
For example all the requests ending with *.do will be handled by the DispatcherServlet.
<web-app>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
Difference between FileSystemResource and ClassPathResource.
In FileSystemResource you need to give the configuration file (i.e. spring-config.xml) relative to your project or the absolute location of the file.
In ClassPathResource spring looks for the file in the ClassPath so configuration (i.e. spring-config.xml) file should be included in the classpath. If spring-config.xml is in classpath, you can simply give the name of the file.
For Example: If your configuration file is at src/main/java/com/test/loadresource then your
FileSystemResource would be:
FileSystemResource resource = new FileSystemResource("src/main/java/com/test/loadresource/spring-config.xml");
And ClassPathResource would be :
ClassPathResource resource = new ClassPathResource("com/test/loadresource /spring-config.xml");
What is inner Bean Definition?
A bean definition added inside the property or constructor-arg elements are called inner bean.
- Example :
<bean id="outerbean" class="...">
<!-- instead of using a reference to a target bean, simply define the target bean inline -->
<property name="targetbean">
<bean class="com.example.Person"> <!-- this is the inner bean -->
<property name="name" value="XYZ"/>
<property name="age" value="35"/>
</bean>
</property>
</bean>
Give examples of how spring platform can be used by an application developer.
The spring platform can be used by an application developer in the following way :
Java method can be made to execute in a database transaction without having to deal with transaction APIs.
Local Java method can be made a remote procedure without having to deal with remote APIs.
Local Java method can be made a management operation without having to deal with JMX APIs.
Local Java method can be made a message handler without having to deal with JMS APIs.
What are the various ways of using spring?
There are various ways and forms in which springs can be used.
They are listed as follows:
1. Full-fledged Spring web app.
2. Spring middle-tier provides help of a third-party web framework.
3. Remote usage scenario: allow the system to be used to remotely use the resources from the server. The remote usage can be done to grab the data from the server or for troubleshooting the environment.
4. EJB -- Wrapping existing POJOs.
Specify the locations where spring can publish its artifacts.
Spring generally publishes its artifacts to four different places:
1. Community download site http://spring.io/downloads/community.
2. Maven Central, which is considered the default repository that Maven queries, and does not require any special configuration to use.
3. The Enterprise Bundle Repository (EBR), which is considered to be run by SpringSource and also hosts all the libraries that integrate with Spring.
4. Public Maven repository hosted on Amazon S3 for the development of snapshots and milestone releases. The jar file names are given in the same form as Maven Central, which makes it a useful place to get development versions of Spring to use with other libraries Spring Framework deployed in Maven Central.
Post a Comment