直接上代码:
<bean id="propertiesFactoryBean" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath*:properties/druid.properties</value> </list> </property> </bean> <context:property-placeholder properties-ref="propertiesFactoryBean"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc-url}"/> <property name="username" value="${username}"/> <property name="password" value="${jdbc-pwd}"/> <property name="initialSize" value="1"/> <property name="minIdle" value="1"/> <property name="maxActive" value="20"/> <property name="maxWait" value="60000"/> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <property name="minEvictableIdleTimeMillis" value="300000"/> <property name="validationQuery" value="select 1 from dual"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <property name="poolPreparedStatements" value="true"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> <property name="filters" value="stat" /> </bean>
上面配置了一个druid数据源,其中url,username,password从属性文件druid.properties读取,该属性文件内容如下:
jdbc-url=jdbc:oracle:thin:@localhost:1521:orcl jdbc-driver=oracle.jdbc.driver.OracleDriver username=test jdbc-pwd=123456
发现死活连接不上,一直提示:
java.sql.SQLException: ORA-01017: invalid username/password; logon denied
但其实用户名、密码是对的,可以用pl/sql developer之类的工具连上oracle,正当快要崩溃的时候,把druid.properties中的username=test,换了个名字,改成:
jdbc-user=test
配置也相应改成:
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc-url}"/>
<property name="username" value="${jdbc-user}"/>
<property name="password" value="${jdbc-pwd}"/>
居然就正常了,建议:属性文件中的key最好不要跟druid的成员名重名。