Results 1 to 7 of 7

Thread: Error: Exception in thread "main" java.lang.IllegalArgumentException: No DataSource

  1. #1

    Default Error: Exception in thread "main" java.lang.IllegalArgumentException: No DataSource

    I am new to Spring and I am trying to learn how to use the JdbcTemplate first. I have a project that I need done ASAP.

    Here is my sample code:

    package com.springinaction.chapter01.hello;

    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;

    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFacto ry;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.jdbc.core.JdbcTemplate;

    public class HelloApp {
    public static void main(String[] args) throws Exception {
    BeanFactory factory =
    new XmlBeanFactory(new ClassPathResource("hello.xml"));

    GreetingService greetingService =
    (GreetingService) factory.getBean("greetingService");

    greetingService.sayGreeting();

    JdbcTemplate jdbc = new JdbcTemplate();
    List commList = jdbc.queryForList("SELECT FIRST FROM NAMES");
    Iterator commIter = commList.iterator();
    while(commIter.hasNext())
    {
    String name = (String)((Map) commIter.next()).get("first");
    System.out.println(name);
    }

    }
    }

    CAN SOMEONE PLEASE TELL ME WHY AM I GETTING THE FOLLOWING ERROR
    Exception in thread "main" java.lang.IllegalArgumentException: No DataSource specified
    at org.springframework.util.Assert.notNull(Assert.jav a:112)
    at org.springframework.jdbc.datasource.DataSourceUtil s.doGetConnection(DataSourceUtils.java:99)
    at org.springframework.jdbc.datasource.DataSourceUtil s.getConnection(DataSourceUtils.java:79)
    at org.springframework.jdbc.core.JdbcTemplate.execute (JdbcTemplate.java:328)
    at org.springframework.jdbc.core.JdbcTemplate.query(J dbcTemplate.java:404)
    at org.springframework.jdbc.core.JdbcTemplate.query(J dbcTemplate.java:412)
    at org.springframework.jdbc.core.JdbcTemplate.queryFo rList(JdbcTemplate.java:443)
    at com.springinaction.chapter01.hello.HelloApp.main(H elloApp.java:23)


    thanks

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    Code:
    Exception in thread "main" java.lang.IllegalArgumentException: 
    No DataSource specified
    post here your database configuration, your DataSource is missing

    regards
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3

    Default Made some change, but still not working

    JAVA CODE:

    package com.springinaction.chapter01.hello;

    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;

    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFacto ry;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.jdbc.core.JdbcTemplate;

    public class HelloApp {
    public static void main(String[] args) throws Exception {
    BeanFactory factory =
    new XmlBeanFactory(new ClassPathResource("hello.xml"));

    /*GreetingService greetingService =
    (GreetingService) factory.getBean("greetingService");

    greetingService.sayGreeting();
    */


    //JdbcTemplate jdbc = new JdbcTemplate();
    JdbcTemplate jdbc = (JdbcTemplate) factory.getBean("jdbcTemplate");

    List commList = jdbc.queryForList("SELECT FIRST FROM NAMES");
    Iterator commIter = commList.iterator();

    while(commIter.hasNext())
    {
    String name = (String)((Map) commIter.next()).get("first");
    System.out.println(name);
    }

    }
    }


    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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="greetingService"
    class="com.springinaction.chapter01.hello.Greeting ServiceImpl">
    <property name="greeting" value="Johnathan!" />
    </bean>

    <bean id="dataSource"
    class="com.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url"
    value="jdbc:oracle:thin:spring/spring@localhost:1521/XE" />
    <property name="username" value="spring" />
    <property name="password" value="spring" />
    <property name="initialSize" value="5" />
    <property name="maxActive" value="15" />
    </bean>

    <bean id="jdbcTemplate"
    class="org.springframework.jdbc.core.JdbcTemplate" >
    <property name="dataSource" ref="dataSource" />
    </bean>

    </beans>


    NEW ERROR:
    Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'jdbcTemplate' defined in class path resource [hello.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.CannotLoadBeanCl assException: Cannot find class [com.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in class path resource [hello.xml]; nested exception is java.lang.ClassNotFoundException: com.apache.commons.dbcp.BasicDataSource
    Caused by: org.springframework.beans.factory.CannotLoadBeanCl assException: Cannot find class [com.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in class path resource [hello.xml]; nested exception is java.lang.ClassNotFoundException: com.apache.commons.dbcp.BasicDataSource
    Caused by: java.lang.ClassNotFoundException: com.apache.commons.dbcp.BasicDataSource
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.springframework.util.ClassUtils.forName(ClassU tils.java:201)
    at org.springframework.beans.factory.support.Abstract BeanDefinition.resolveBeanClass(AbstractBeanDefini tion.java:327)
    at org.springframework.beans.factory.support.Abstract BeanFactory.resolveBeanClass(AbstractBeanFactory.j ava:1075)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:346)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 51)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:156)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:248)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:160)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:261)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:109)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1099)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:861)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:421)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 51)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:156)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:248)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:160)
    at com.springinaction.chapter01.hello.HelloApp.main(H elloApp.java:24)


    it looks like it cant find com.apache.commons.dbcp.BasicDataSource but its ni my project?

    PLEASE HELP

  4. #4
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'jdbcTemplate' defined in class path resource [hello.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource';
    i have a simple question, how many context files, you have?
    i see one hello.xml, what all your db configuration is in another conext file right?
    for instance db-context.xml (apply your name file)

    so
    Code:
    new XmlBeanFactory(new ClassPathResource("hello.xml"));
    is wrong

    instead
    Code:
    new XmlBeanFactory(new ClassPathResource("hello.xml","db-context.xml") );
    of course use your path if is there one

    THT
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  5. #5

    Default Everything in Hello.XML

    I have everything in my hello.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="greetingService"
    class="com.springinaction.chapter01.hello.Greeting ServiceImpl">
    <property name="greeting" value="Johnathan!" />
    </bean>

    <bean id="dataSource"
    class="com.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url"
    value="jdbc:oracle:thin:spring/spring@localhost:1521/XE" />
    <property name="username" value="spring" />
    <property name="password" value="spring" />
    <property name="initialSize" value="5" />
    <property name="maxActive" value="15" />
    </bean>

    <bean id="jdbcTemplate"
    class="org.springframework.jdbc.core.JdbcTemplate" >
    <property name="dataSource" ref="dataSource" />
    </bean>

    </beans>

    do i need anything else

  6. #6
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Your creating the JdbcTemplate programmatically, hence the dataSource isn't injected. You need to look up the jdbcTemplate from Spring or make sure you set the dataSource on the jdbcTemplate.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  7. #7

    Smile I Got It Work, Yahoo

    Just so everyone would know that I just got it working. The issue was in the setup of the datasource bean. I had "com.apache.commons.dbcp.BasicDataSource" not "org.apache.commons.dbcp.BasicDataSource"

    what a stupid error but i would not do it again!

    thanks to everyone!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •