Reputation: 587
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>
Upvotes: 1
Views: 22861
Reputation: 1906
This is a very simple and a frequently occurring error while using spring. The problem is that your IDE is not finding a proper class to create bean object for a service, repository or an entity while building the project. Make sure that you have defined the proper bean name for the classes. If the error still occurs, try refreshing your application by selecting the project and clicking 'F5' button or by closing and opening the project. You can do Maven Update in eclipse. By right clicking on the project > Maven > Update Project.
Regards
Upvotes: 0
Reputation: 5959
Add this to your spring xml
<bean name="testDao" class="com.test.dao.java.TestDao" />
Upvotes: 2
Reputation: 564
on top of
<context:component-scan base-package="com.test.main"/> to <context:component-scan base-package="com.test"/>
following
@Repository
public class TestDao{
* @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();
}
}
}
Should solve your issue
Cheers Anant
Upvotes: 0
Reputation: 241
change
<context:component-scan base-package="com.test.main"/>
to
<context:component-scan base-package="com.test"/>
Upvotes: 1
Reputation: 69440
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"/>
Upvotes: 3