Spring专题

Spring面试问题

上页

  1. bean注入的两种类型是什么?
    有两种,按照Setter注入,和按照构造器注入。

  2. 什么是自动匹配Auto wiring?
    Spring框架将为你寻找需要注入的类自动 注入。
    <beans>
    <bean id="bar" class="com.act.Foo" Autowire="autowire type"/>
    </beans>

  3. Autowire的类型有几个?
    1. byName
    2. byType
    3. constructor
    4. autodetect

  4. ApplicationContext事件和相关的监听者是什么?
    下面这些都是org.springframework.context.Application-Event.子类:
    1. ContextClosedEvent – 当上下文关闭是激活
    2. ContextRefreshedEvent – 当上下文初始化或刷新时激活。
    3. RequestHandledEvent – 当上下文处理请求时激活

  5. Spring AOP有哪些概念?
    见: AOP模式

  6. Spring中的Advice类型有几个?
    1. Around : 拦截目标方法所有调用
    2. Before : 在目标方法被调用之前调用
    3. After : 在目标方法被调用之后调用
    4. Throws : 当目标方法throws exception时调用
    5. Around : org.aopalliance.intercept.MethodInterceptor
    6. Before : org.springframework.aop.BeforeAdvice
    7. After : org.springframework.aop.AfterReturningAdvice
    8. Throws : org.springframework.aop.ThrowsAdvice

  7. AutoProxying有几种类型?
    1. BeanNameAutoProxyCreator
    2. DefaultAdvisorAutoProxyCreator
    3. Metadata autoproxying

  8. Spring中有关所有Exception的Exception是什么?
    DataAccessException – org.springframework.dao.DataAccessException

  9. DAO抛出的Exception是什么?
    SQLException 它是DataAccessException子类

  10. DataAccessException是什么?
    它是RuntimeException,并不强迫一定要处理。

  11. 如何配置从JNDI中获得一个DataSource?
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
    <value>java:comp/env/jdbc/myDatasource</value>
    </property>
    </bean>

  12. 如何创建一个Spring的DataSource连接池?
    <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>

  13. JDBC如何在Spring使用更有效率?
    使用Spring的JdbcTemplate模板模式。

  14. JdbcTemplate如何使用?
    JdbcTemplate template = new JdbcTemplate(myDataSource);

    public class StudentDaoJdbc implements StudentDao {
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
    }
    more..
    }

    配置:
    <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>


  15. 如何使用JDBCTemplate写数据库SQL操作?
    PreparedStatementCreator:

    PreparedStatement createPreparedStatement(Connection conn)

    throws SQLException;


    BatchPreparedStatementSetter:

    setValues(PreparedStatement ps, int i) throws SQLException;

    int getBatchSize();

  16. 解释为什么要使用RowCallbackHandler ?

    我们通常使用ResultSet逐行浏览记录,但是Spring提供这个接口由用户决定处理哪个具体一行. 这是spring的 isRowCallbackHandler提供.processRow() 方法需要实现,需要对每行记录做什么事情。

#面试      #java学习路线Java  

Spring各种源码项目下载

返回Spring专题首页