码迷,mamicode.com
首页 > 数据库 > 详细

Java Spring用properties配置数据库连接池出现错误的处理方法[图]

时间:2020-02-21 10:13:00      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:bcd   match   nbsp   data   span   enum   红色   mysql   conf   

错误

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 17 in XML document from class path resource [db-config.xml] is invalid; 
nested exception is org.xml.sax.SAXParseException; lineNumber: 17; columnNumber: 78; cvc-complex-type.2.4.c:
The matching wildcard is strict, but no declaration can be found for element contest:property-placeholder.

 

保存数据库用户名等信息的properties文件

mysql-username=root
mysql-password=abcd2020
mysql-jdbcUrl=jdbc:mysql://192.168.3.16/beers
mysql-driverClass=com.mysql.jdbc.Driver

 

 

Java的测试类

package com.mars.springtest;

import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.SQLException;

public class BeerTest {
    ConfigurableApplicationContext ioc=new ClassPathXmlApplicationContext("db-config.xml");
    @Test
    public void test() throws SQLException {
       DataSource bean= (DataSource) ioc.getBean("mysql-pool");
        System.out.println(bean.getConnection());
    }
}

 

出错时的spring的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:contest="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">


    <contest:property-placeholder location="classpath:db-config.properties"/>
    <bean id="mysql-pool" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${mysql-username}"></property>
        <property name="password" value="${mysql-password}"></property>
        <property name="jdbcUrl" value="${mysql-jdbcUrl}"></property>
        <property name="driverClass" value="${mysql-driverClass}"></property>
    </bean>
</beans>

 

更正之后的spring的配置文件。注意红色的两行默认是没有,加上这两行就Ok了

<?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:contest="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">

    <contest:property-placeholder location="classpath:db-config.properties"/>
    <bean id="mysql-pool" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${mysql-username}"></property>
        <property name="password" value="${mysql-password}"></property>
        <property name="jdbcUrl" value="${mysql-jdbcUrl}"></property>
        <property name="driverClass" value="${mysql-driverClass}"></property>
    </bean>
</beans>

 

技术图片

 

后记

2020年2月21日 农历正月2020年正月二八 星期五 凌晨3点 上海 晴

Java Spring用properties配置数据库连接池出现错误的处理方法[图]

标签:bcd   match   nbsp   data   span   enum   红色   mysql   conf   

原文地址:https://www.cnblogs.com/majestyking/p/12340028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!