Spring 没有名为“的bean”;userDao";定义

Spring 没有名为“的bean”;userDao";定义,spring,javabeans,applicationcontext,Spring,Javabeans,Applicationcontext,我得到了这个例外。这是我的代码: ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:/servlet-context.xml"); UserDao userDao = (UserDao) context.getBean("userDao"); 这是我的servlet-context.xml文件,位于/myproject/WebContent/WEB-INF/spring/appServlet/

我得到了这个例外。这是我的代码:

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中

<?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
将从应用程序的类路径根加载每个
servlet context.xml
资源

然而,
/myproject/WebContent/WEB-INF/spring/appServlet/servlet context.xml根本不在类路径根上,因此spring将忽略您的文件并构建一个空上下文

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


或者,将其移动到
WEB-INF/classes/spring/appServlet/servlet context.xml
,并将代码更改为
classpath*:/spring/appServlet/servlet context.xml
classpath*:/servlet context.xml
将从应用程序的类路径根加载每个
servlet context.xml
资源

然而,
/myproject/WebContent/WEB-INF/spring/appServlet/servlet context.xml根本不在类路径根上,因此spring将忽略您的文件并构建一个空上下文

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


或者,将其移动到
WEB-INF/classes/spring/appServlet/servlet context.xml
,并将代码更改为
classpath*:/spring/appServlet/servlet context.xml
getBean(JdbcUserDao.class)
?另外,您确定正在加载正确的文件吗?让它成为语法不正确的XML,看看Spring是否会抱怨。奇怪的是,你能试试
getBean(jdbcusedao.class)
?另外,您确定正在加载正确的文件吗?将其设置为语法不正确的XML,并查看Spring是否会抱怨。