在控制器上使用@Autowired时出现NoSuchBeanDefinitionException

时间:2019-02-20 17:53:10

标签: java spring hibernate model-view-controller

我在春季遇到豆类问题。当我尝试在服务器上运行此消息时,出现错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'rio.antelodel.david.ejercicios_programacion.Model.DAO.PersonaDAO' available

这些是我的课程:

@Controller
public class PersonaController {

    @Autowired
    private PersonaDAO personaDAO;

    @RequestMapping("/persona")
    public ModelAndView getPersonasList () {

        ...

    }

}

这里是PersonaDAO

@Repository
public class PersonaDAO extends AHibernateDAO < Persona > {

    public Persona find (String key) {

        return getCurrentSession().get(getCurrentClass(), key);

    }

}

这是我的spring上下文文件

<?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:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<context:annotation-config/>
<tx:annotation-driven transaction-manager="txManager"/>

<bean id = "txManager" class = "org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name = "sessionFactory" ref = "sessionFactory" />
</bean>

<bean id= "dataSource" class= "org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name = "driverClassName" value = "com.mysql.cj.jdbc.Driver"/>
    <property name = "url" value = "jdbc:mysql://localhost:3306/gestion_ejercicios_programacion?useSSL=false&amp;serverTimezone=UTC"/>
    <property name = "username" value = "root"/>
    <property name = "password" value = "mysqlpass"/>

</bean>

<bean id = "sessionFactory" class = "org.springframework.orm.hibernate5.LocalSessionFactoryBean">

    <property name = "dataSource" ref = "dataSource"/>
    <property name = "packagesToScan" value = "rio.antelodel.david.ejercicios_programacion."/>

    <property name = "hibernateProperties">
        <props>
            <prop key = "dialect">org.hibernate.dialect.Derbydialect</prop>
        </props>
    </property>

</bean>

<bean id = "personaController" class = "rio.antelodel.david.ejercicios_programacion.Controller.PersonaController" />

<bean id = "alumnoDAO" class = "rio.antelodel.david.ejercicios_programacion.Model.DAO.AlumnoDAO" />
<bean id = "archivoDAO" class = "rio.antelodel.david.ejercicios_programacion.Model.DAO.ArchivoDAO" />
<bean id = "categoriaDAO" class = "rio.antelodel.david.ejercicios_programacion.Model.DAO.CategoriaDAO" />
<bean id = "dificultadAlumnoEjercicioDAO" class = "rio.antelodel.david.ejercicios_programacion.Model.DAO.DificultadAlumnoEjercicioDAO" />
<bean id = "dificultadDAO" class = "rio.antelodel.david.ejercicios_programacion.Model.DAO.DificultadDAO" />
<bean id = "ejercicioDAO" class = "rio.antelodel.david.ejercicios_programacion.Model.DAO.EjercicioDAO" />
<bean id = "ejercicioIdeaDAO" class = "rio.antelodel.david.ejercicios_programacion.Model.DAO.EjercicioIdeaDAO" />
<bean id = "personaDAO" class = "rio.antelodel.david.ejercicios_programacion.Model.DAO.PersonaDAO" />
<bean id = "practicaDAO" class = "rio.antelodel.david.ejercicios_programacion.Model.DAO.PracticaDAO" />
<bean id = "practicaEvaluacionDAO" class = "rio.antelodel.david.ejercicios_programacion.Model.DAO.PracticaEvaluacionDAO" />
<bean id = "profesorDAO" class = "rio.antelodel.david.ejercicios_programacion.Model.DAO.ProfesorDAO" />
<bean id = "pruebaEvaluacionDAO" class = "rio.antelodel.david.ejercicios_programacion.Model.DAO.PruebaEvaluacionDAO" />
<bean id = "titulacionDAO" class = "rio.antelodel.david.ejercicios_programacion.Model.DAO.TitulacionDAO" />

</beans>

我已经测试并确保错误在控制器的@Autowired行中。

问题是,我的bean是在上下文文件中定义的,如果我在本地运行一个仅获取所述bean的主类(PersonaDAO pDAO = context.getBean(PersonaDAO.class);),它的工作原理就很好,因此就像我的应用程序仅在感觉到时就标识该bean。感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您正在用context:annotation-config重新定义XML中的相同bean,例如@Controller@Repository bean应该被自动扫描。仅使用一种机制来注册bean。