1

I am a beginner in spring. Here is my 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>

And DB.properties is given below:

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

I am getting this error given below:

        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

I know what is this error. This error is about class BasicDataSource. This class is not found...... but i already included the required jars in build path in my project. Jars is as given below...... a)/home/praveen/Downloads/commons-dbcp-1.4.jar b)/home/praveen/Downloads/org.apache.commons.pool.jar

I have read some where that may be the version of jars causing the problem... and interfering with each other.....

The jars are already available in project build path.... Please help me to solve this problem. Any help would be appreciated by me....

The RunClass.java is given below:

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();
        }

    }
}
3
  • Where on the classpath is your DB.properties resource? Commented Apr 4, 2014 at 18:12
  • It is in my src folder of the project in eclipse IDE Commented Apr 4, 2014 at 18:15
  • Show us your RunClass class. How are you loading your beans.xml context? Commented Apr 4, 2014 at 18:18

3 Answers 3

2

If you are not using maven, make sure if your DB.properties is in location as below:

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

else specify your full classpath for file location as:

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

and then change your code as pointed by @Sotirios and @Ramesh:

    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    BasicDataSource bds = (BasicDataSource) ctx.getBean("myDataSource");
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, you figured it out.
And yet he accepted an answer that didn't work for him instead of yours.
1

Check your jars once, is spring-expression.jar there?

And create applicationContext like below and check

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

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

15 Comments

It is not loading your DB.properties file. Can you check once.
After applying your code... i am getting error given below
Exception in "main" java.lang.NoClassDefFoundError: org/springframework/expression/PropertyAccessor at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:538) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:439) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83).RunClass.main(RunClass.java:19)
Caused by: java.lang.ClassNotFoundException: org.springframework.expression.PropertyAccessor
add spring-expression.jar to your classpath
|
1

XmlBeanFactory is a deprecated class since 3.1 and a BeanFactory. A BeanFactory is only responsible for generating beans. The task of processing (or rather registering the processes for processing) beans belongs to ApplicationContext subclasses.

Therefore use ClassPathXmlApplicationContext to produce your beans and retrieve them

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.