ApplicationContext ctx=new FileSystemXmlApplicationContext("applicationContext.xml");
Datasource ds = (Datasource) ctx.getBean("dataSource");

解决方案 »

  1.   

    spring的注入有三种方法,setter注入,构造函数和静态工厂方法
    一般用setter注入,在listTopMsg这个方法所在的类中加入DataSource的get set方法
    在你的spring配置文件中
    <bean id="" class="">
       <ref bean name="dataSource"/>
    </bean>
      

  2.   

    我用这种办法已试过了,,测试说得不到连接,,我debug时看到dataSource里有信息,
    我写了个测试类,是这样的:
     public void testListTopMsg() throws DataAccessException {     
            ApplicationContext context =   new FileSystemXmlApplicationContext("dataAccessContext.xml");
            
            messageDaoImpl  = (MessageDaoImpl)context.getBean("msgDao");        dataSource = (DataSource)context.getBean("dataSource");
        
            messageDaoImpl.setDataSource(dataSource);
            List actualReturn =messageDaoImpl.listTopMsg("tr0001");        Iterator iter = actualReturn.iterator();
            hs = new HashMap();
            lastValue = new HashMap();
            while (iter.hasNext()) {
                CompCgsMessage seq = (CompCgsMessage) iter.next();
                hs.put(seq.getFieldName().trim(), seq);
                System.out.println(seq.getFieldName().trim());
            }      
        }当运行到 processRow(ResultSet rs)时,报
    org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory
    org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory, cause: 
    COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver] CLI0106E  连接关闭。 SQLSTATE=08003
    哪位大侠帮我看看是怎么回事呢??
      

  3.   

    myth822(枫红一刀) 说的,我都已做啦
      

  4.   

    不使用spring的JDBC模板试试可不可以
      

  5.   

    myth822(枫红一刀) ,
    没懂你的意思.
      

  6.   

    单独使用JDBC的dataSource的getConnection
      

  7.   

    那应该没问题啊,我数据库是好好的啊.直接用jdbc不可能用问题啊
      

  8.   

    很简单啊,只要你在Spring的配置文件中把datasource设置好,然后在程序中直接用ApplicationContext context =   new FileSystemXmlApplicationContext("dataAccessContext.xml");
    调用就行了,你的这个是没有问题的,但是下面就不用在getBean("datasource")了,没有必要,程序中从来就不会有和数据库有关系的代码,都已经用ORM搞定了,只要你的DAO继承HibernateDaoSupport就可以了。
      

  9.   

    楼主的做法没有什么问题啊,他也没有用hibernate
      

  10.   

    我不想用hibernate,想支接用Spring的jdbc..