spring 使用外部配置文件访问数据库

1.Caused by: java.lang.ClassNotFoundException: com.mchange.v2.ser.Indirector
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>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值