没有名为"userDao"的bean;被定义为 [英] no bean named "userDao" is defined

查看:84
本文介绍了没有名为"userDao"的bean;被定义为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个例外.这是我的代码:

I got this exception. This is my code:

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

这是我的servlet-context.xml文件,位于/myproject/WebContent/WEB-INF/spring/appServlet/servlet-context.xml

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>

我该如何解决?

推荐答案

classpath*:/servlet-context.xml将从应用程序类路径的 root 中加载每个servlet-context.xml资源.

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根本不在类路径的根目录上,因此Spring将忽略您的文件并构建一个空上下文.

/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.

尝试将servlet-context.xml移到WEB-INF/classes的正下方(即移至WEB-INF/classes/servlet-context.xml).

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

或者,将其移至WEB-INF/classes/spring/appServlet/servlet-context.xml,然后将代码更改为classpath*:/spring/appServlet/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

这篇关于没有名为"userDao"的bean;被定义为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆