public static Object getBean(String name)
    {
        if(context == null)
            context = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
        return context.getBean(name);
    }
    protected CallableStatement prepareCall(String sql, String jndiName)
        throws SQLException
    {
        log.debug("prepareCall(String sql, String jndiName) start");
        long startTime = System.currentTimeMillis();
        DataSource ds = (DataSource)SpringContextHolder.getBean(jndiName);
        if(ds != null)
            throw new RuntimeException("There no dataousrce matching to given  jndi name : " + jndiName);
        CallableStatement cs = ds.getConnection().prepareCall(sql);
        long endTime = System.currentTimeMillis();
        if(log.isDebugEnabled())
            log.debug("end prepareCall()  Execution time: " + (endTime - startTime) + " ms.");
        return cs;
    }这个是传过去一个调用存储过程的一个方法的样子,,,,

解决方案 »

  1.   

    首先弄清一下 WebApplicationContextUtils、SpringContextHolder 这几个类是哪来的。
      

  2.   


     public static Object getBean(String name)
        {
            if(context == null)
                context = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
            return context.getBean(name);
        }
    这一段就是取得你在spring中配置的相应的bean,通过方法名也可知,传入的就是要取得的bean名
    这与自己新建一个类实现ApplicationContextAware这个接口,并在新建的类中定义一个ApplicationContext
    的域一样,当对你的spring配置的所有bean解析完以后,就会自动把你的所有bean的上下文全部注入到这个类里。
      

  3.   


    protected CallableStatement prepareCall(String sql, String jndiName) throws SQLException {
            log.debug("prepareCall(String sql, String jndiName) start");
            long startTime = System.currentTimeMillis(); // 获得执行前系统当前毫秒时间
            DataSource ds = (DataSource) SpringContextHolder.getBean(jndiName); 
            // SpringContextHolder
            // 应该就是包含你上一段代码的类,因为你的上一个方法是静态的,
            // 而jndiName就是你在spring配置文件中建立的bean,从而获得一个数据源        if (ds != null)
                throw new RuntimeException("There no dataousrce matching to given  jndi name : "
                        + jndiName);
            CallableStatement cs = ds.getConnection().prepareCall(sql); // 返回一个存储过程的语句
            long endTime = System.currentTimeMillis(); // 获得执行后系统当前毫秒时间
            if (log.isDebugEnabled())
                log.debug("end prepareCall()  Execution time: " + (endTime - startTime) + " ms."); // 执行方法的时间
            return cs;
        }
      

  4.   

    WebApplicationContextUtils.getRequiredWebApplicationContext返回一个ApplicationContext,
    ApplicationContext就是一个beanFactory,只不过是可配置的,也就是表现形式是个配置文件(ApplicationContext.xml)。
    context.getBean(name)根据提供的bean的name创建一个实例
    由这句DataSource ds = (DataSource)SpringContextHolder.getBean(jndiName);看,这次创建了一个DataSource 对象ds。
    然后使用ds.getConnection().prepareCall(sql);返回一个CallableStatement 对象cs。