Monday, September 7, 2009

SPRING interview Questions

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

 

What are the different types of AutoProxying?

         BeanNameAutoProxyCreator

         DefaultAdvisorAutoProxyCreator

         Metadata autoproxying

 

What is the Exception class related to all the exceptions that are thrown in spring applications?

DataAccessException - org.springframework.dao.DataAccessException.

 

What kind of exceptions those spring DAO classes throw?

The spring’s DAO class does not throw any technology related exceptions such as SQLException. They throw exceptions which are subclasses of DataAccessException.

 

What is DataAccessException?

DataAccessException is a Runtime Exception. This is an Unchecked Exception. The user is not forced to handle these kinds of exceptions.

 

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>

 

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>

 

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.

 

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.

 

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>

 

 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 isPreparedStatementCreator and the other interface is BatchPreparedStatementSetter.

 

1

2

3

4

5

6

7

8

9

10

 

No comments:

Post a Comment