at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 19 more
错误原因:缺少mchange-commons-java-0.2.3.4.jar包
2.数据库账号密码不正确会导致连接失败
3.配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="869140"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///test"></property>
</bean>
</beans>
4.测试文件
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("spring_scope.xml");
javax.sql.DataSource data = (javax.sql.DataSource) ctx.getBean("dataSource");
System.out.println("测试");
System.out.println(data.getConnection());
ctx.close();
5.使用外部配置文件db.properties
user=root
password=869140
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql:///test
在bean配置文件中添加如下属性
<context:property-placeholder location="classpath:db.properties" />
报错:The prefix "context" for element "context:property-placeholder" is not bound.
解决办法是添加xmlns:context="http://www.springframework.org/schema/context"
再次报错:Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 8 in XML document from class path resource [spring_scope.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:property-placeholder'.
解决办法:在xsi:schemaLocation 中添加
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
解决后的bean配置文件如下:
<?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-2.5.xsd">
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverclass}"></property>
<property name="jdbcUrl" value="${jdbcurl}"></property>
</bean>
</beans>