xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- PropertyPlaceholderConfigurer 使用外部属性文件 -->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<bean id="dbSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
</bean>
</beans>
db.properties外部属性文件
user=lm
password=lm
driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
java类
package com.test.main;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JdbcMain_1 {
public static void main(String[] args) throws SQLException {
ApplicationContext ac = new ClassPathXmlApplicationContext("outerproperty.xml");
BasicDataSource ds = (BasicDataSource) ac.getBean("dbSource");
System.out.println(ds.getConnection());
}
}
当执行住方法时就会出现
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbSource' defined in class path resource [outerproperty.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: Lorg/apache/commons/pool/impl/GenericObjectPool;
的问题
参照网上别的博主的博客将commons.pool.jar加载到classpath(其实这个jar包eclipse的plugins文件夹下有的)但还是会出现同样的问题,然后找了半天还是不知道问题在哪儿,最后我又重新下了一个版本为1.5的commons.pool.jar包(以前是1.6的)下载网址为http://maven.ibiblio.org/maven2/commons-pool/commons-pool/1.5/,然后就没出现这个问题了,那就应该时版本不适合的问题了。但是接着出现了下面的问题:
Exception in thread "main" org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (ORA-01017: invalid username/password; logon denied。
再打印下来就是对应属性的用户名了,然后问题就解决了。