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

Posted 国王陛下万万岁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Spring用properties配置数据库连接池出现错误的处理方法[图]相关的知识,希望对你有一定的参考价值。

错误

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配置数据库连接池出现错误的处理方法[图]的主要内容,如果未能解决你的问题,请参考以下文章

jsp怎么获取spring配置文件properties中的信息

Spring之Environment

java中用Properties类加载配置文件

springboot的一些配置

spring数据源文件jdbc.properties发生改变,不重启服务的状态下,如何让新的配置文件生效?

Spring Properties整理