Java 例外:没有这样的bean定义

Java 例外:没有这样的bean定义,java,spring,Java,Spring,这是我的主要类,它调用bean testDao Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testDao' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultLi

这是我的主要类,它调用bean testDao

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)
“这是>>>测试DAO文件”

这是springNew.xml

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

扫描包“com.test.main”中的bean。您的bean位于包
com.test.dao.java

更改:

<?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>


扫描包“com.test.main”中的bean。您的bean位于包
com.test.dao.java

更改:

<?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>


更改

更改

<context:component-scan base-package="com.test.dao"/>
应该解决你的问题

干杯!在你的头顶上

<context:component-scan base-package="com.test.dao"/>
应该解决你的问题


干杯!请将此添加到您的spring xml中

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


       }
        }

将其添加到spring xml中

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


       }
        }

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


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



问候

你能粘贴一段你的springNew.xml吗?@AnantLaxmikantBobde-我已经在你的
springNew.xml中向下滚动了
没有定义为
testDao
@MohammadFaisal的bean-我已经给出了我主要类的包名你能粘贴一段你的springNew.xml吗?@AnantLaxmikantBobde-我已经有了滚动条在您的
springNew.xml
中没有定义为
testDao
@MohammadFaisal的bean-我已经给出了我的主类的包名,但是我的主类在包com.test.main中。应该对带有
@Component、@Service、@Repository
等注释的bean(类)执行组件扫描。所以com.test可以做到这一点,您也可以设置com.test.dao。但是我的主要类在包com.test.main中。应该对带有
@Component、@Service、@Repository
等注释的bean(类)执行组件扫描。因此com.test可以完成这项任务,您也可以设置com.test.dao.sea,我在main中扫描bean,在main中我已经给出了getbean,它将重定向到testDao@PalakChugh认为您不了解组件扫描。在这里,你必须添加你的bean所在的路径。好的,你确定吗?因为在这之前我已经做过了。它工作了。你能提供我它所在的位置的文档吗written@PalakChugh在这里,我扫描main中的bean,在main中,我给出了getbean,它将重定向到testDao@PalakChugh认为您不了解组件扫描。在这里,你必须添加你的bean所在的路径。好的,你确定吗?因为在这之前我已经做过了。它工作了。你能提供我它所在的位置的文档吗written@PalakChugh看这里
<bean name="testDao" class="com.test.dao.java.TestDao" />