繁体   English   中英

org.apache.commons.dbcp.SQLNestedException:无法加载JDBC驱动程序类'$ {driver}'

[英]org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${driver}'

我是春天的初学者。 这是我的beans.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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="DB.properties" />
    </bean>

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${uname}"/>
        <property name="password" value="${pwd}"/>
    </bean>
</beans>

DB.properties如下所示:

#database connection propertiess
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/Payment
uname=root
pwd=renu@193

我收到以下错误消息:

        Apr 04, 2014 11:07:25 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${driver}'
    at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1429)
    at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    at com.student.spring.test.RunClass.main(RunClass.java:22)
Caused by: java.lang.ClassNotFoundException: ${driver}

    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1420)
    ... 3 more

我知道这是什么错误。 此错误与类BasicDataSource有关。 找不到该类……但是我已经在项目中的构建路径中包含了所需的jar。 Jars如下所示... a)/home/praveen/Downloads/commons-dbcp-1.4.jar b)/home/praveen/Downloads/org.apache.commons.pool.jar

我已经读过一些可能是引起问题的罐子版本...并互相干扰.....

这些罐子已经在项目构建路径中可用。...请帮助我解决此问题。 任何帮助,我将不胜感激。

下面给出了RunClass.java:

package com.student.spring.test;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

@SuppressWarnings("deprecation")
public class RunClass {
    public static void main(String[] args) {
        Resource resource = new ClassPathResource("beans.xml");
        BeanFactory factory = new XmlBeanFactory(resource);


        BasicDataSource bds = (BasicDataSource) factory.getBean("myDataSource");        
        Connection connection;
        try {
            connection = bds.getConnection();
            System.out.println(bds.getDriverClassName());
            System.out.println(bds.getUrl());
            System.out.println(bds.getUsername());
            System.out.println(bds.getPassword());
            connection.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

如果您不使用Maven,请确保DB.properties是否位于以下位置:

    src-|-package1
        |-package2
        .
        .
        .
        |-packageN
        |-beans.xml
        |-DB.properties

否则,将文件位置的完整类路径指定为:

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:packageName.jdbc1.properties" />
</bean>

然后按照@Sotirios和@Ramesh的指示更改代码:

    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    BasicDataSource bds = (BasicDataSource) ctx.getBean("myDataSource");

检查一下罐子,那里是spring-expression.jar吗?

并创建如下的applicationContext并检查

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

BasicDataSource bds = (BasicDataSource) context.getBean("myDataSource");  

XmlBeanFactory是从3.1开始不推荐使用的类,并且是BeanFactory BeanFactory仅负责生成bean。 处理(或注册用于处理的进程)bean的任务属于ApplicationContext子类。

因此,请使用ClassPathXmlApplicationContext生成您的bean并检索它们

ApplicationContext ctx = new ClassPathXmlApplicationContext("test.xml");
BasicDataSource bds = (BasicDataSource) ctx.getBean("myDataSource");  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM