数据库连接datasource的配置

来源:互联网 发布:阿里云ecs打开80端口 编辑:程序博客网 时间:2024/05/18 00:04
<!-- 1.配置数据库相关参数properties的属性,引入外部配置文件,必须 -->

    <context:property-placeholder location="classpath:jdbc.properties" />


<!-- 2.数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!-- 配置连接池属性 -->
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />


        <!-- dbcp私有配置属性 -->
        <property name="initialSize" value="${jdbc.initialSize}" />
        <property name="maxActive" value="${jdbc.maxActive}" />
        <!-- 关闭连接后不自动commit -->
        <property name="maxIdle" value="${jdbc.maxIdle}" />
        <!-- 获取连接超时时间 -->
        <property name="minIdle" value="${jdbc.minIdle}" />
        <!-- 当获取连接失败重试次数 -->
        <property name="maxWait" value="${jdbc.maxWait}" />
    </bean>

需要注意的是:driverClassName,url,username......这些属性名一定要严格定义的,今天之前摘自别人,结果写法有误,导致报错,说是无法创建bean,查找了

好久才找到原因。

把错误摘下来,

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in file [E:\work\workspace04\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\MVNLearn\WEB-INF\classes\spring-dao.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'driverClass' of bean class [org.apache.commons.dbcp.BasicDataSource]: Bean property 'driverClass' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1518)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)