복붙노트

[SPRING] applicationContext가 서블릿 컨텍스트에 대한 컨트롤러를 찾지 못했습니다.

SPRING

applicationContext가 서블릿 컨텍스트에 대한 컨트롤러를 찾지 못했습니다.

applicationContext.xml 및 dispatcher-servlet.xml 구성을 가진 Spring 웹 응용 프로그램이 있습니다. applicationContext.xml에서 을 정의했지만 애플리케이션을 실행할 때 을 dispatcher-servlet.xml에 추가하지 않으면 컨트롤러를 찾을 수 없습니다. 두 가지 모두에서 동일한 기본 패키지를 사용하므로 문제가되지 않습니다.

applicationContext.xml이 dispatcher-servlet.xml의 부모라고 생각 했으므로 혼란 스럽습니다. applicationContext.xml에 을 넣지 않을까요?

을 포함한다.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

편집 : 나는 또한 mvc : annotation-driven dispatcher-servlet.xml을 사용하고 있는데,이 컨트롤러는 Controllers를 선택해야한다고 생각했다.

편집 2 : 여기에 설정 파일이 있습니다. applicationContext.xml에서 Spring Security 및 OAuth 설정을 제거했습니다 (보안상의 이유로 인해 어쨌든 관련이 없습니다).

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:sec="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context" xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
      http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="bar.foo"/>
<context:property-placeholder location="classpath:my.properties" />
<bean class="bar.foo.ServicesConfig" />

</beans>

dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="bar.foo.controller" />
<mvc:annotation-driven/>
<mvc:default-servlet-handler />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    <property name="order" value="2" />
</bean>

<bean id="contentViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
        </map>
    </property>
    <property name="defaultViews">
        <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
    </property>
    <property name="order" value="1" />
</bean>

</beans>

편집 3 : 좋아, 이것은 흥미 롭습니다. 내 서비스 및 DAO 클래스는 웹 프로젝트에서 참조하는 다른 프로젝트 (JAR)에 있습니다. Java 기반 구성을 사용하고 applicationContext.xml에서이를 참조하고 있습니다.

<bean class="bar.foo.config.ServicesConfig" />

즉, 이는 웹 프로젝트 (applicationContext.xml이있는 곳)에 Controller 주석 만 있다는 것을 의미합니다. 되돌아 보면, contextController를 제거하면 applicationContext.xml에서 component-scan이 영향을 미치지 않아야한다. @Controller에 대한 주석을 제외하고는 주석이 없기 때문이다. (FIX to EDIT : @Autowired annotation이있다.) 그러나 applicationContext.xml에서 컨텍스트 : component-scan을 제거하면 Dispatcher 서블릿 검색에서 찾은 컨트롤러에서 내 Service 클래스를 찾을 수 없다는 메시지가 표시됩니다. ServicesConfig에 대한 참조로 충분하지 않아야합니까? 다음은 참조 용 ServicesConfig 클래스입니다. ApplicationContext.xml에서 스캔 한 것과 다른 패키지 인 서비스에 대한 자체 구성 요소 스캔이 있습니다.

@Configuration
@ComponentScan({ "some.other.package", "another.package" })
@ImportResource({ "classpath:commonBeans.xml" })
@PropertySource({ "classpath:services.properties",
"classpath:misc.properties" })
public class ServicesConfig {
  // Bean definitions //
}

해결책:

컨텍스트를 제거했을 때 루트 컨텍스트에서 구성 요소 검사를 실행하면 컨트롤러가 자동 무선 서비스 빈을 선택하지 않습니다. 이것은 루트 컨텍스트가 서비스 기반 Java 기반 config Bean을 참조하기 때문입니다. 그러나 구성 요소를 검색하기위한 루트 컨텍스트 설정이 없었습니다. 따라서 루트 컨텍스트 (applicationContext.xml)에 구성 요소 검색을 추가하면 모든 것이 작동합니다. 여기에 내가 지금 가지고있는 것이있다.

applicationContext.xml :

<bean class="bar.foo.config.ServicesConfig" />
<context:component-scan base-package="bar.foo.config" />

dispatcher-servlet.xml :

<context:component-scan base-package="bar.foo.controller" />

컨트롤러, Autowired 및 컨트롤러 패키지의 다른 주석을 픽업 할 웹 컨텍스트 설정이 있습니다. 이것이 모범 사례인지 확실하지 않습니다.

해결법

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

    1.맞습니다 - ContextLoaderListener에 의해로드 된 루트 애플리케이션 컨텍스트 (ServletContext가 초기화되는 시점)와 웹 컨텍스트 (DispatcherServlet에 의해로드 됨)의 두 가지 애플리케이션 컨텍스트가 있으며 루트 애플리케이션 컨텍스트는 웹의 부모입니다 문맥.

    맞습니다 - ContextLoaderListener에 의해로드 된 루트 애플리케이션 컨텍스트 (ServletContext가 초기화되는 시점)와 웹 컨텍스트 (DispatcherServlet에 의해로드 됨)의 두 가지 애플리케이션 컨텍스트가 있으며 루트 애플리케이션 컨텍스트는 웹의 부모입니다 문맥.

    이제 이들은 서로 다른 두 가지 응용 프로그램 컨텍스트이므로 서로 다르게 작동합니다. 응용 프로그램 컨텍스트에서 서비스에 대한 구성 요소 검사를 정의하면 여기에 서비스의 모든 Bean이 만들어집니다.

    Dispatcher 서블릿이로드되면 웹 컨텍스트 생성이 시작됩니다 (에 의해 구동 됨) 그러면 uri의 핸들러 메소드에 대한 매핑이 생성되어 애플리케이션의 빈 목록이 생성됩니다 컨텍스트 (루트 응용 프로그램 컨텍스트가 아닌 웹 응용 프로그램 컨텍스트) 및 구성 요소 정의가 없으므로 여기에서 컨트롤러 관련 빈을 찾을 수없고 매핑이 만들어지지 않습니다. Dispatcher 서블릿 컨텍스트에서 구성 요소 검색을 정의 할 수도 있습니다.

    루트 응용 프로그램 컨텍스트에서 컨트롤러 관련 bean을 제외하는 것이 좋습니다.

    <context:component-scan base-package="package">
        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>
    

    웹 응용 컨텍스트에서 관련된 컨트롤러 만 해당 :

    <context:component-scan base-package="package" use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
    </context:component-scan>
    
  2. ==============================

    2.우리의 응용 프로그램에서는 dispatcher-servlet.xml에 정의합니다.

    우리의 응용 프로그램에서는 dispatcher-servlet.xml에 정의합니다.

    나는 그것이 그것이 applicationContext.xml보다는 오히려 있어야한다고 생각한다.

    Spring 문서의이 섹션은 더 많은 정보를 제공해야한다.

    http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html

    16.2 절의 다이어그램 중 하나에서 볼 수 있듯이 디스패처 - 서블릿은 컨텍스트 계층에서 applicationContext 위에 위치합니다.

  3. ==============================

    3.나는 동일한 문제가 있었고이 튜토리얼과 web.xml 코드를 비교하여 변경했다. 여기에 내 web.xml 파일이 있습니다.

    나는 동일한 문제가 있었고이 튜토리얼과 web.xml 코드를 비교하여 변경했다. 여기에 내 web.xml 파일이 있습니다.

    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xsi:schemaLocation="
        http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/business-config.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    context-param과 listener는 제가 놓친 것입니다.

    나는 그것이 당신을 돕기를 바랍니다.

  4. from https://stackoverflow.com/questions/11453530/applicationcontext-not-finding-controllers-for-servlet-context by cc-by-sa and MIT license