Tolal:56 Click:
1
2 3
Spring Interview Questions And Answers
Page 1
Ques: 1 What is a BeanFactory?
Ans:
The BeanFactory is provide an advanced configuration mechanism capable of managing beans of any kind of storage facility. The ApplicationContext builds on top of the BeanFactory and adds other functionality like integration with Springs AOP features, message resource handling for use in internationalization, event propagation, declarative mechanisms to create the ApplicationContext and optional parent contexts, and application-layer specific contexts such as the WebApplicationContext, among other enhancements.
Ques: 2
What are the benefits of the Spring Framework transaction management ?
Ans:
Spring Framework supports:
* Programmatic transaction management.
* Declarative transaction management.
Spring provides a unique transaction management abstraction, which enables a consistent programming model over a variety of underlying transaction technologies, such as JTA or JDBC. Supports declarative transaction management. Provides a simpler API for programmatic transaction management than a number of complex transaction APIs such as JTA. Integrates very well with Spring's various data access abstractions.
Ques: 3
Explain about RowCallbackHandler and why it is used?
Ans:
RowCallbackHandler interface is used by JdbcTemplate for processing rows of a ResultSet on a per-row basis. Implementations of this interface perform the actual work of processing each row but don't need to worry about exception handling. SQLExceptions will be caught and handled by the calling JdbcTemplate. RowCallbackHandler object is typically stateful: It keeps the result state within the object, to be available for later inspection.
RowCallbackHandler interface has one method :
void processRow(ResultSet rs) :- Implementations must implement this method to process each row of data in the ResultSet.
Ques: 4
Explain about BatchPreparedStatementSetter?
Ans:
BatchPreparedStatementSetter interface sets values on a PreparedStatement provided by the JdbcTemplate class for each of a number of updates in a batch using the same SQL. Implementations are responsible for setting any necessary parameters. SQL with placeholders will already have been supplied. Implementations of BatchPreparedStatementSetter do not need to concern themselves with SQLExceptions that may be thrown from operations they attempt. The JdbcTemplate class will catch and handle SQLExceptions appropriately.
BatchPreparedStatementSetter has two method:
* int getBatchSize() :- Return the size of the batch.
* void setValues(PreparedStatement ps, int i) :-Set values on the given PreparedStatement.
Ques: 5
Explain about PreparedStatementCreator?
Ans:
The PreparedStatementCreator interface is a callback interfaces used by the JdbcTemplate class. This interface creates a PreparedStatement given a connection, provided by the JdbcTemplate class. Implementations are responsible for providing SQL and any necessary parameters. A PreparedStatementCreator should also implement the SqlProvider interface if it is able to provide the SQL it uses for PreparedStatement creation. This allows for better contextual information in case of exceptions.
It has one method:
PreparedStatement createPreparedStatement(Connection con) throws SQLException
Create a statement in this connection. Allows implementations to use PreparedStatements. The JdbcTemplate will close the created statement.
Ques: 6
How can you create a DataSource connection pool?
Ans:
To 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>
Ques: 7
How can you configure JNDI instead of datasource in spring applicationcontext.xml?
Ans:
You can configure JNDI instead of datasource in spring applicationcontext.xml using "org.springframework.jndi.JndiObjectFactoryBean". For Example:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/appfuse</value>
</property>
</bean>
Ques: 8
How do you configure your database driver in spring?
Ans:
To configure your database driver using datasource "org.springframework.jdbc.datasource.DriverManagerDataSource". For Example:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:db/appfuse</value>
</property>
<property name="username"><value>sa</value></property>
<property name="password"><value></value></property>
</bean>
Ques: 9
How does Spring supports DAO in hibernate?
Ans:
Spring’s HibernateDaoSupport class is a convenient super class for Hibernate DAOs. It has handy methods you can call to get a Hibernate Session, or a SessionFactory. The most convenient method is getHibernateTemplate(), which returns a HibernateTemplate. This template wraps Hibernate checked exceptions with runtime exceptions, allowing your DAO interfaces to be Hibernate exception-free.
Ques: 10
What classes are used to Control the database connection in Spring's JDBC API?
Ans:
The classes that are used to Control the database connection in Spring's JDBC API are :
* DataSourceUtils
* SmartDataSource
* AbstractDataSource
* SingleConnectionDataSource
* DriverManagerDataSource
* TransactionAwareDataSourceProxy
* DataSourceTransactionManager
Ques: 11
Name the JDBC Core classes to control basic JDBC processing and error handling in Spring's JDBC API?
Ans:
the JDBC Core classes to control basic JDBC processing and error handling in Spring's JDBC API are :
* JdbcTemplate
* NamedParameterJdbcTemplate
* SimpleJdbcTemplate (Java5)
* DataSource
* SQLExceptionTranslator
Ques: 12 What is NamedParameterJdbcTemplate class used for in Spring's JDBC API?
Ans:
The NamedParameterJdbcTemplate class adds support for programming JDBC statements using named parameters (as opposed to programming JDBC statements using only classic placeholder ('?') arguments. The NamedParameterJdbcTemplate class wraps a JdbcTemplate, and delegates to the wrapped JdbcTemplate to do much of its work.
Ques: 13 What is JdbcTemplate class used for in Spring's JDBC API?
Ans:
The JdbcTemplate class is the central class in the JDBC core package. It simplifies the use of JDBC since it handles the creation and release of resources. This class executes SQL queries, update statements or stored procedure calls, imitating iteration over ResultSets and extraction of returned parameter values. It also catches JDBC exceptions defined in the hierarchy of org.springframework.dao package. Code using the JdbcTemplate only need to implement callback interfaces, giving them a clearly defined contract.
The PreparedStatementCreator callback interface creates a prepared statement given a Connection provided by this class, providing SQL and any necessary parameters. The JdbcTemplate can be used within a DAO implementation via direct instantiation with a DataSource reference, or be configured in a Spring IOC container and given to DAOs as a bean reference.
Example:
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 code here
}
Ques: 14
List the Springs's DAO exception hierarchy?
Ans:
Springs's DAO exception hierarchy is :
* CleanupFailureAccessException
* DataAccessResourceFailureException
* DataIntegrityViolationException
* DataRetrievalFailureException
* DeadlockLoserDataAccessException
* IncorrectUpdateSemanticsDataAccessException
* InvalidDataAccessApiUsageException
* InvalidDataAccessResourceUsageException
* OptimisticLockingFailureException
* TypeMismatchDataAccessException
* UncategorizedDataAccessException
Ques: 15 Explain the Spring's DataAccessException?
Ans:
Spring's org.springframework.dao.DataAccessException extends the extends NestedRuntimeException which in turn extends and RuntimeException. Hence DataAcccessException is a RuntimeException so there is no need to declare it in the method signature.
Ques: 16
Explain DAO in Spring framework?
Ans:
Spring Framework's Data Access Object (DAO) support provides integration of heterogeneous Java Database Connectivity (JDBC) and Object-Relational Mapping (ORM) products. DAO support is provided for JDBC, Hibernate, iBATIS, Java Data Objects (JDO), Java Persistence API (JPA), Oracle's TopLink and Common Client Interface (CCI).
Ques: 17
What is Metadata autoproxying?
Ans:
Spring also supprts autoproxying driven by metadata. Metadata autoproxy configuration is determined by source level attributes and keeps AOP metadata with the source code that is being advised. This lets your code and configuration metadata in one place.
The most common use of metadata autoproxying is for declarative transaction support.
Ques: 18 Explain BeanNameAutoProxyCreator and DefaultAdvisorAutoProxyCreator classes?
Ans:
BeanNameAutoProxyCreator generates proxies for beans that match a set of names. This name matching is similar to the NameMethodMatcherPointcut which allows for wildcard matching on both ends of the name. This is used to apply an aspect or a group of aspects uniformly across a set of beans that follow a similar naming conventions.
The DefaultAdvisorAutoProxyCreator is the more powerful autoproxy creator. Use this class to include it as a bean in your BeanFactory configuration. It implemnts the BeanPostProcessor interface. DefaultAdvisorAutoProxyCreator only works with advisors.
Ques: 19
What is Autoproxying in Spring?
Ans:
Spring has a facility of autoproxy that enables the container to generate proxies for us. We create autoproxy creator beans. Spring provides two classes to support this:
* BeanNameAutoProxyCreator
* DefaultAdvisorAutoProxyCreator
Ques: 20
What is ProxyFactoryBean? Describe its properties?
Ans:
ProxyFactoryBean creates proxied objects. Like other JavaBeans, it has properties that control its behaviour. ProxyFactoryBean properties are:
* target : The target bean of the proxy.
* proxyInterface : A list of interfaces that should be implemented by the proxy.
* interceptorNames : The bean names of the advice to be applied to the target.
* singleton : Whether the factory should return the same instance of the proxy for each getBean invocation.
* aopProxyFactory : The implemetation of the ProxyFactoryBean interface to be used.
* exposeProxy : Whether the target class should have access to the current proxy. This is done by calling AopContext.getCurrentProxy.
* frozen : Whether changes can be made to the proxy's advice once the factory is created.
* optimize : Whether to aggressively optimize generated proxies.
* proxyTargetClass : Whether to proxy the target class, rather than implementing an interface.
Goto Page:
1
2 3
Spring Objective
Spring Objective Questions And Answers
Spring Interview Questions And Answers
Spring Subjective Questions And Answers
R4R,Spring Objective, Spring Subjective, Spring Interview Questions And Answers,Spring,Spring Interview,Spring Questions ,Spring Answers