2009-11-18 2 views
3

서블릿 이름이 "spring-mvc"인 스프링 디스패처 서블릿이 있습니다. 다음과 같이 스프링 MVC-servlet.xml 파일이 나타납니다 : 나는 주석 스캐너가 정의되어 WEB-INF/주석-context.xml에있는 파일에서글로벌 컨텍스트 컨텍스트가있는 Spring MVC 주석 : component-scan?

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

<bean 
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> 

<bean 
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 

. 주석이 달린 모든 클래스가로드되고 다른 스프링 빈은로드 할 수 있습니다.

그러나 경로 매핑은 spring-mvc에서 작동하지 않습니다. context-scanner를 spring-mvc-servlet.xml에 복사하면 작동합니다.

spring-mvc-servlet.xml이 전역 스프링 레벨로 정의 된 bean을 참조 할 수 있습니까?

+0

에는 '글로벌 스프링 수준'과 같은 것이 없습니다. 이'WEB-INF/annotation-context.xml' 파일은 무엇을하고 무엇을 의미합니까? – skaffman

+0

전역 스프링 수준 == 응용 프로그램 컨텍스트 (디스패처 서블릿이 정의 된 중첩 컨텍스트와 반대) – jsight

답변

2

load your contexts hierarchically을 사용하면 annotation-context.xml에 설명 된 컨텍스트가 Spring MVC 컨텍스트의 부모가됩니다. 그러면 후자는 전자에서 정의 된 모든 빈에 액세스 할 수 있습니다.

Spring documenation에는 여러 가지 방법이 설명되어 있습니다. 예를 들어 web.xml :

// load parent context 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/annotation-context.xml</param-value> 
</context-param> 

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

// load Spring MVC context 
<servlet> 
    <servlet-name>spring-mvc</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
+1

예, 정확히 내가하는 일입니다. Spring-mvc 서블릿의 컨텍스트에서 스프링 mvc는 부모 컨텍스트의 컨트롤러를 볼 수 없습니다. 하지만 수동으로 호출 할 수는 있지만 dispatcherservlet은 해당 경로 매핑을 볼 수 없습니다. – jsight

관련 문제