1

I am trying an example to connect to database using jdbc and spring and getting the below exception. Please advise.

exception:

Aug 17, 2017 10:44:51 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6422b8ff: startup date [Thu Aug 17 22:44:51 IST 2017]; root of context hierarchy
Aug 17, 2017 10:44:51 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'daoClass' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:638)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:979)
    at org.manjosh.main.JdbcMain.main(JdbcMain.java:14)

main class:

package org.manjosh.main;

import org.manjosh.dao.DaoClass;
import org.manjosh.model.Circle;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JdbcMain {

    public static void main(String[] args){

        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");

        DaoClass dao = ctx.getBean("daoClass",DaoClass.class);

        Circle circle = dao.getCirle(1);
        System.out.println(circle.getName());
        }

}

dao class:

package org.manjosh.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.manjosh.model.Circle;
import org.springframework.stereotype.Component;
@Component
public class DaoClass {

    public Circle getCirle(int circleId){

        String driver = "oracle.jdbc.driver.OracleDriver";
        Connection conn = null;

        try{
        //step1 load the driver class  
        Class.forName(driver).newInstance();

        //step2 create  the connection object  
        conn =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");  

        //step3 create the statement object  
        PreparedStatement stmt=conn.prepareStatement("SELECT * FROM circle where ID =?");
        stmt.setInt(1, circleId);

        Circle circle = null;

        //step4 execute query  
        ResultSet rs=stmt.executeQuery();

        while(rs.next()){
            circle = new Circle(rs.getInt(circleId),rs.getString("name"));
            }
        rs.close();
        stmt.close();
        return circle;
            }
        catch(Exception e){
        throw new RuntimeException(e);
        }
        finally {
            try{
        conn.close();
                }
            catch(SQLException e){} 
            }   
        }

    }

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

    <context:annotation-config/>
    <context:component-scan base-package = "org.manjosh.main"/>




</beans>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
manjosh
  • 438
  • 6
  • 28

2 Answers2

3

In spring.xml, you have defined component scan configuration as <context:component-scan base-package = "org.manjosh.main"/> which will scan all classes under org.manjosh.main. As your DaoClass is in org.manjosh.dao, spring will not create bean of DaoClass hence exception NoSuchBeanDefinitionException is thrown.

Change <context:component-scan base-package = "org.manjosh.main"/> to <context:component-scan base-package = "org.manjosh.main,org.manjosh.main"/>

Rohan
  • 3,068
  • 1
  • 20
  • 26
1

<context:component-scan> scans packages to find and register beans within the application context.

To resolve NoSuchBeanDefinitionException issue please make below change in spring.xml file

Replace

<context:component-scan base-package = "org.manjosh.main"/>

with

<context:component-scan base-package = "org.manjosh.dao,org.manjosh.main"/>

OR

<context:component-scan base-package = "org.manjosh.dao"/>

OR

<context:component-scan base-package = "org.manjosh.*"/>

OR

<context:component-scan base-package = "*"/>
PMerlet
  • 2,568
  • 4
  • 23
  • 39