异常没有这样的bean定义

时间:2016-01-13 10:10:37

标签: java spring

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)

这是我的主要类,它正在调用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());
    }
}

"这是>>> testDao文件"

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

这是>>>> 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>

5 个答案:

答案 0 :(得分:2)

您扫描包“com.test.main”以查找bean。你的bean在包<context:component-scan base-package="com.test.main"/>

变化:

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

{{1}}

答案 1 :(得分:2)

将其添加到spring xml

<bean name="testDao" class="com.test.dao.java.TestDao" />

答案 2 :(得分:1)

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

答案 3 :(得分:0)

之上
<context:component-scan base-package="com.test.main"/> to  <context:component-scan base-package="com.test"/>
以下

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


       }
        }

应解决您的问题

Cheers Anant

答案 4 :(得分:0)

使用弹簧时,这是一个非常简单且经常出现的错误。问题是您的IDE在构建项目时没有找到适当的类来为服务,存储库或实体创建bean对象。确保已为类定义了正确的bean名称。如果错误仍然存​​在,请尝试通过选择项目并单击“F5”按钮或关闭并打开项目来刷新应用程序。您可以在eclipse中进行Maven更新。通过右键单击项目&gt; Maven&gt;更新项目。

此致