no bean named “userDao” is defined

那年仲夏 提交于 2019-12-02 13:30:03

问题


I got this exception. This is my code:

ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:/servlet-context.xml");
UserDao userDao = (UserDao) context.getBean("userDao");

And this is my servlet-context.xml file located in /myproject/WebContent/WEB-INF/spring/appServlet/servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <beans:bean id="userDao" class="it.ex.home.dao.JdbcUserDao">
        <beans:property name="dataSource" ref="dataSource"/>
    </beans:bean>

    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <beans:property name="url" value="jdbc:mysql://localhost:3306/hibernate_db"/>
        <beans:property name="username" value="root"/>
        <beans:property name="password" value="root"/>
    </beans:bean>

    <context:component-scan base-package="it.ex.home" />

</beans:beans>

How can I fix this?


回答1:


classpath*:/servlet-context.xml will load every servlet-context.xml resource from the root of your application's classpath.

/myproject/WebContent/WEB-INF/spring/appServlet/servlet-context.xml, however, isn't on the classpath root at all, and so Spring will ignore your file and build an empty context.

Try moving servlet-context.xml to be directly under WEB-INF/classes (i.e. to WEB-INF/classes/servlet-context.xml).

Alternatively, move it to WEB-INF/classes/spring/appServlet/servlet-context.xml, and change your code to classpath*:/spring/appServlet/servlet-context.xml



来源:https://stackoverflow.com/questions/9605657/no-bean-named-userdao-is-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!