Spring Interview Questions: Spring Is A Lightweight Inversion of Control and Aspect-Oriented Container Framework
Spring Interview Questions: Spring Is A Lightweight Inversion of Control and Aspect-Oriented Container Framework
x
x
1) What is Spring?
Answer:
Spring is a lightweight inversion of control and aspect-oriented container framework.
2) Explain Spring?
Answer:
Lightweight : Spring is lightweight when it comes to size and transparency. The basic version of spring
framework is around 1MB. And the processing overhead is also very negligible.
Inversion of control (IoC) : Loose coupling is achieved in spring using the technique Inversion of Control.
The objects give their dependencies instead of creating or looking for dependent objects.
Aspect oriented (AOP) : Spring supports Aspect oriented programming and enables cohesive development
by separating application business logic from system services.
Container : Spring contains and manages the life cycle and configuration of application objects.
Framework : Spring provides most of the intra functionality leaving rest of the coding to the developer.
x
x
x
x
x
x
x
x
x
x
Answer:
The AOP module is used for developing aspects for our Spring-enabled application. Much of the support has
been provided by the AOP Alliance in order to ensure the interoperability between Spring and
other AOP frameworks. This module also introduces metadata programming to Spring. Using Springs
metadata support, we will be able to addannotations to our source code that instruct Spring on where and how
to apply aspects.
8) What is JDBC abstraction and DAO module?
Answer:
Using this module we can keep up the database code clean and simple, and prevent problems that result from a
failure to close database resources. A new layer of meaningful exceptions on top of the error messages given by
several database servers is bought in this module. In addition, this module uses Springs AOP module to
provide transaction management services for objects in a Spring application.
9) What are object/relational mapping integration module?
Answer:
Spring also supports for using of an object/relational mapping (ORM) tool over straight JDBC by providing the
ORM module. Spring provide support to tie into several popular ORM frameworks, including Hibernate, JDO,
and iBATIS SQL Maps. Springs transaction management supports each of these ORM frameworks as well
as JDBC.
10) What is web module?
Answer:
This module is built on the application context module, providing a context that is appropriate for web-based
applications. This module also contains support for several web-oriented tasks such as transparently handling
multipart requests for file uploads and programmatic binding of request parameters to your business objects. It
also contains integration support with Jakarta Struts.
11) What is web module?
Answer:
Spring comes with a full-featured MVC framework for building web applications. Although Spring can easily
be integrated with other MVC frameworks, such as Struts, Springs MVC framework uses IoC to provide for a
clean separation of controller logic from business objects. It also allows you to declaratively bind request
parameters to your business objects. It also can take advantage of any of Springs other services, such as I18N
messaging and validation.
Answer:
These applications are like any Java application. They are made up of several classes, each performing a
specific purpose within the application. But these classes are configured and introduced to each other through
an XML file. This XML file describes how to configure the classes, known as the Spring configuration file.
16) What is XMLBeanFactory?
Answer:
BeanFactory has many implementations in Spring. But one of the most useful one
isorg.springframework.beans.factory.xml.XmlBeanFactory, which loads its beans based on the definitions
contained in an XML file. To create an XmlBeanFactory, pass a java.io.InputStream to the constructor.
TheInputStream will provide the XML to the factory. For example, the following code snippet uses a
java.io.FileInputStream to provide a bean definition XML file to XmlBeanFactory.
BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml"));
To retrieve the bean from a BeanFactory, call the getBean() method by passing the name of the bean you want
to retrieve.
x
x
x
1.
2.
3.
4.
5.
6.
7.
x
x
x
x
x
x
x
x
x
x
37) What are the different points where weaving can be applied?
Answer:
Compile Time
Classload Time
Runtime
x
x
x
x
x
x
x
x
x
x
x
<property name="jndiName">
<value>java:comp/env/jdbc/myDatasource</value>
</property>
</bean>
44) How can you create a DataSource connection pool?
Answer:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driver">
<value>${db.driver}</value>
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
</bean>
45) How JDBC can be used more efficiently in spring framework?
Answer:
JDBC can be used more efficiently with the help of a template class provided by spring framework called
asJdbcTemplate.
46) How JdbcTemplate can be used?
Answer:
With use of Spring JDBC framework the burden of resource management and error handling is reduced a lot.
So it leaves developers to write the statements and queries to get the data to and from the database.
JdbcTemplate template = new JdbcTemplate(myDataSource);
A simple DAO class looks like this.
public class StudentDaoJdbc implements StudentDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
more..
}
The configuration is shown below.
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="studentDao" class="StudentDaoJdbc">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
</bean>
<bean id="courseDao" class="CourseDaoJdbc">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
</bean>
47) How do you write data to backend in spring using JdbcTemplate?
Answer:
The JdbcTemplate uses several of these callbacks when writing data to the database. The usefulness you will
find in each of these interfaces will vary. There are two simple interfaces. One
is PreparedStatementCreator and the other interface is BatchPreparedStatementSetter.
48) Explain about PreparedStatementCreator?
Answer:
PreparedStatementCreator is one of the most common used interfaces for writing data to database. The interface
has one method createPreparedStatement().
PreparedStatement createPreparedStatement(Connection conn)
throws SQLException;
When this interface is implemented, we should create and return a PreparedStatement from the Connection
argument, and the exception handling is automatically taken care off. When this interface is implemented,
another interfaceSqlProvider is also implemented which has a method called getSql() which is used to provide
sql strings to JdbcTemplate.
49) Explain about BatchPreparedStatementSetter?
Answer:
If the user what to update more than one row at a shot then he can go for BatchPreparedStatementSetter.
This interface provides two methods
setValues(PreparedStatement ps, int i) throws SQLException;
int getBatchSize();
The getBatchSize() tells the JdbcTemplate class how many statements to create. And this also determines how
many times setValues() will be called.
50) Explain about RowCallbackHandler and why it is used?
Answer:
In order to navigate through the records we generally go for ResultSet. But spring provides an interface that
handles this entire burden and leaves the user to decide what to do with each row. The interface provided by
spring isRowCallbackHandler. There is a method processRow() which needs to be implemented so that it
is applicable for each and everyrow.
void processRow(java.sql.ResultSet rs);