Dead End Dead End - 3 months ago 11
Java Question

Exception no such bean defination

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testDao' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1083)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:274)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1079)
at com.test.main.java.TestMain.main(TestMain.java:30)


this is my main class which is calling bean testDao .

public class TestMain {

/**
* @param args
* @throws SQLException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws SQLException,
ClassNotFoundException, FileNotFoundException, NullPointerException {

ApplicationContext ctx = new ClassPathXmlApplicationContext(
"springNew.xml");
TestDao dao = ctx.getBean("testDao", TestDao.class);

Test test = dao.getTest(1);
System.out.println(test.getName());

}

}


"THIS is >>>testDao file"

package com.test.dao.java;

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

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.test.java.Test;

@Component
public class TestDao {
static PreparedStatement ps;
ResultSet rs;
Connection conn = null;

private Connection getConnection() throws SQLException,
ClassNotFoundException, FileNotFoundException, NullPointerException {

if (conn == null) {
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/testdb", "postgres",
"postgres");
conn.close();
} catch (Exception e) {
e.printStackTrace();

}
}
return conn;

}

/**
* @param testId
* @return
* @throws SQLException
* @throws ClassNotFoundException
* @throws NullPointerException
* @throws FileNotFoundException
*/
public Test getTest(int testId) throws SQLException,
ClassNotFoundException, FileNotFoundException, NullPointerException {

conn = getConnection();
try {

conn = getConnection();
ps = conn
.prepareStatement("SELECT * FROM testdb.testtab where id =?");
ps.setInt(1, testId);
Test test = null;
rs = ps.executeQuery();
if (rs.next()) {
test = new Test(testId, rs.getString("name"));
}

return test;
} finally {
rs.close();
ps.close();
conn.close();
}

}
}


AND this is >>>>springNew.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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd "
xmlns:context="http://www.springframework.org/schema/context">

<!-- <context-annotation-config/> -->
<context:component-scan base-package="com.test.main"/>
</beans>

Answer

You scan the package "com.test.main" for beans. You bean is in package com.test.dao.java

Change:

<context:component-scan base-package="com.test.main"/>

to

<context:component-scan base-package="com.test.dao"/>