我是春天的初学者。这是我的 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。 罐子如下...... 一)/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();
}
}
}
答案 0 :(得分:2)
如果您不使用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");
答案 1 :(得分:1)
检查你的罐子一次,是spring-expression.jar吗?
并创建如下的applicationContext并检查
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
BasicDataSource bds = (BasicDataSource) context.getBean("myDataSource");
答案 2 :(得分:1)
XmlBeanFactory
是自3.1以来的弃用类和BeanFactory
。 BeanFactory
仅负责生成bean。处理(或者更确切地说,注册处理过程)bean的任务属于ApplicationContext
子类。
因此,使用ClassPathXmlApplicationContext
生成bean并检索它们
ApplicationContext ctx = new ClassPathXmlApplicationContext("test.xml");
BasicDataSource bds = (BasicDataSource) ctx.getBean("myDataSource");