Spring interview questions and answers
81 What is an Advice?
Advice is the implementation of an aspect. It is something like
telling your application of a new behavior. Generally, and advice is inserted
into an application at joinpoints.
82 What is a
Pointcut?
A pointcut is something that defines at what joinpoints an
advice should be applied. Advices can be applied at any joinpoint that is
supported by the AOP framework. These Pointcuts allow you to specify where
theadvice can be applied.
83 What is an
Introduction in AOP?
An introduction allows the user to add new methods or attributes
to an existing class. This can then be introduced to an existing class without
having to change the structure of the class, but give them the new behavior and
state.
84 What is a
Target?
A target is the class that is being advised. The class can be a
third party class or your own class to which you want to add your own custom
behavior. By using the concepts of AOP, the
target class is free to center on its major concern, unaware to any
advice that is being applied.
85 . What is a Proxy?
A proxy is an object that is created after applying advice to a
target object. When you think of client objects the target object and the proxy
object are the same.
86 .What is meant by
Weaving?
The process of applying aspects to a target object to create a
new proxy object is called as Weaving. The aspects are woven intothe target
object at the specified joinpoints.
87 What are the
different points where weaving can be applied?
- Compile Time
- Classload Time
- Runtime
88 . What are the
different advice types in spring?
- Around :
Intercepts the calls to the target method
- Before : This is
called before the target method is invoked
- After : This is
called after the target method is returned
- Throws : This is
called when the target method throws and exception
- Around :
org.aopalliance.intercept.MethodInterceptor
- Before :
org.springframework.aop.BeforeAdvice
- After :
org.springframework.aop.AfterReturningAdvice
- Throws :
org.springframework.aop.ThrowsAdvice
89 What are the different
types of AutoProxying?
- BeanNameAutoProxyCreator
- DefaultAdvisorAutoProxyCreator
- Metadata
autoproxying
90
What kind of exceptions those spring DAO classes throw?
The
spring DAO class does not throw any technology related exceptions such as SQLException.
They throw exceptions which are subclasses of DataAccessException.
91
What is DataAccessException?
DataAccessException
is a RuntimeException. This is an Unchecked Exception. The user is not forced
to handle these kinds of exceptions.
92
How can you configure a bean to get DataSource from JNDI?
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/myDatasource</value>
</property>
</bean>
93
How can you create a DataSource connection pool?
<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>
94
How JDBC can be used more efficiently in spring framework?
JDBC
can be used more efficiently with the help of a template class provided by
spring framework called as JdbcTemplate.
95
How JdbcTemplate can be used?
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.
A simple DAO class looks like this.
public class StudentDaoJdbc implements StudentDao {
private JdbcTemplate jdbcTemplate;
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
more..
}
The configuration is shown below.
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>
<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>
96
How do you write data to backend in spring using JdbcTemplate?
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.
97
Explain about PreparedStatementCreator?
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 interface SqlProvider is also implemented which has a method called getSql() which is used to provide sql strings to JdbcTemplate.
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 interface SqlProvider is also implemented which has a method called getSql() which is used to provide sql strings to JdbcTemplate.
98
Explain about BatchPreparedStatementSetter?
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.
int getBatchSize();
The getBatchSize() tells the JdbcTemplate class how many statements to create. And this also determines how many times setValues() will be called.
99.
Explain about RowCallbackHandler and why it is used?
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 is
RowCallbackHandler. 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);
100
What is JDBC abstraction and DAO module?
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 Spring’s AOP module to provide
transaction management services for objects in a Spring application.
·
0 comments:
Post a Comment