복붙노트

[SPRING] "userDao"라는 bean이 정의되지 않았습니다.

SPRING

"userDao"라는 bean이 정의되지 않았습니다.

이 예외가 있습니다. 이것은 내 코드입니다.

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

그리고 이것은 /myproject/WebContent/WEB-INF/spring/appServlet/servlet-context.xml에있는 내 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>

이 문제를 어떻게 해결할 수 있습니까?

해결법

  1. ==============================

    1.classpath * : / servlet-context.xml은 응용 프로그램의 클래스 경로의 루트에서 모든 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로 변경하십시오

  2. from https://stackoverflow.com/questions/9605657/no-bean-named-userdao-is-defined by cc-by-sa and MIT license