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">
&lt;contest:property-placeholder location="classpath:db-config.properties"/&gt;
&lt;bean id="mysql-pool" class="com.mchange.v2.c3p0.ComboPooledDataSource"&gt;
    &lt;property name="user" value="${mysql-username}"&gt;&lt;/property&gt;
    &lt;property name="password" value="${mysql-password}"&gt;&lt;/property&gt;
    &lt;property name="jdbcUrl" value="${mysql-jdbcUrl}"&gt;&lt;/property&gt;
    &lt;property name="driverClass" value="${mysql-driverClass}"&gt;&lt;/property&gt;
&lt;/bean&gt;

</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">
&lt;contest:property-placeholder location="classpath:db-config.properties"/&gt;
&lt;bean id="mysql-pool" class="com.mchange.v2.c3p0.ComboPooledDataSource"&gt;
    &lt;property name="user" value="${mysql-username}"&gt;&lt;/property&gt;
    &lt;property name="password" value="${mysql-password}"&gt;&lt;/property&gt;
    &lt;property name="jdbcUrl" value="${mysql-jdbcUrl}"&gt;&lt;/property&gt;
    &lt;property name="driverClass" value="${mysql-driverClass}"&gt;&lt;/property&gt;
&lt;/bean&gt;

</beans>

 

 

后记

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