我写的一个功能以jar包形式放在spring的框架中,我想获得spring配置中的datasource并将它保持住,可以多次getConnection来给我的程序使用,应该如何做?请给出代码片段,谢谢。

解决方案 »

  1.   

    再applicationContext.xml中配置如下:<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
    <property name="url"
    value="jdbc:oracle:thin:@。。" />
    <property name="username" value="" />
    <property name="password" value="" />
    <property name="poolPreparedStatements" value="true" />
    <property name="defaultAutoCommit" value="false" />
    </bean>
      

  2.   


     <JDBCConnectionPool 
            MaxCapacity="20" Name="ctaisPool"       
            Properties="user=padiskf;protocol=thin"
            SupportsLocalTransaction="true" Targets="myserver"
            URL="jdbc:oracle:thin:@IP:1521:database"/>在其他模块中引用这个 Name="ctaisPool" 就行了
      

  3.   

    <bean id="dataSource"
            class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
            <property name="url"
                value="jdbc:oracle:thin:@。。" />
            <property name="username" value="" />
            <property name="password" value="" />
            <property name="poolPreparedStatements" value="true" />
            <property name="defaultAutoCommit" value="false" />
    </bean>
      

  4.   

     <bean id="dataSource"
              class="org.springframework.jdbc.datasource.DataSourceUtils"
              factory-method="getDataSourceFromJndi">
            <constructor-arg><value>jdbc/blog</value></constructor-arg>
        </bean>
        <bean id="accountManager" class="AccountManager">
            <property name="dataSource">
                <ref bean="dataSource" />
            </property>
        </bean>
      

  5.   

    对的,在spring中你可以以bean的形式配置datasource,在需要这个datasource的地方,只要注入这个bean就可以了。
      

  6.   

    spring的配置我都明白,但我的代码是不在spring框架里的,但在一个web项目中,想获得这个datasource,是要调用类似WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean("");这样的东西
      

  7.   

    有谁能给一个使用的范例么?servletContext如何得到?