IT

ContextLoaderListener?

lottoking 2020. 7. 12. 09:02
반응형

ContextLoaderListener?


(루 또는 "스프링 MVC 프로젝트"템플릿에 의해 생성) 표준 스프링 웹 응용 프로그램과 web.xml 파일을 생성 ContextLoaderListener하고 DispatcherServlet. 왜 구축을 사용 DispatcherServlet하여 전체 구성을로드하도록 만들지 보장합니까?

ContextLoaderListener를 사용하여 웹과 관련되지 않은 항목을로드하고 DispatcherServlet을 사용하여 웹 관련 항목 (컨트롤러 등)을로드한다는 것을 이해합니다. 결과적으로 부모와 유사라는 두 가지 파생가 생생합니다.

배경 :

나는 몇 년 동안이 표준 방식으로 있습니다.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Handles Spring requests -->
<servlet>
    <servlet-name>roo</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring/webmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

이로 인해 발생하는 종종 두 가지 발생과 그 사이의 오류에 문제가 발생했습니다. 과거에는 항상 솔루션을 구축 할 수 있습니다. / 아키텍처가 항상 더 나아진다는 느낌이 들었습니다. 그러나 지금 나는 두 상황의 사건에 관한 문제에 직면하고 있다 .

-그러나 이것은 두 가지 새로운 패턴을 다시 생각하게 만듭니다. 왜 나 자신 에게이 문제를 제기 해야하는지, 왜 모든 스프링 구성 파일을 하나의 파일로로드하지 않고 완전히 DispatcherServlet제거 하지 않는지 스스로 묻고 ContextLoaderListener있습니다. (여전히 다른 구성 파일이 많은 것들이 있습니다.)

를 제거하지 않을 이유가 ContextLoaderListener있습니까?


귀하의 경우에는, 아니, 이유가해야 우리 할 우리 없다 ContextLoaderListener하고 applicationContext.xml. 앱이 제대로 작동하면 더 간단합니다.

그렇습니다. 일반적으로 권장되는 패턴은 웹이 아닌 내용을 웹앱 수준의 자주에 유지하는 것이지만 약 규칙에 지나지 않습니다.

webapp 고유의 유일한 이유는 다음과 가변적입니다.

  • DispatcherServlet서비스를 공유해야하는 여러 개가있는 경우
  • 스프링 유선 서비스에 액세스해야하는 레거시 / 스프링 이외의 서블릿이있는 경우
  • 당신은 서블릿 필터가있는 경우 그 웹 애플리케이션 레벨의 서비스에 후크 (예를 들어 스프링 시큐리티의 DelegatingFilterProxy, OpenEntityManagerInViewFilter등)

이 중 어느 것도

등의 예약 된 작업, JMS 연결, 서블릿의 추가 할 때 같은 백그라운드 작업까지 추가 할 때 추가하는 것을 잊지 않을 경우 그냥 조심 <load-on-startup>당신에 web.xml, 다음 발음이 작업은 서블릿의 첫 번째 액세스 할 때 시작되지 않습니다.


다른 방법으로 응용 프로그램을 구성 할 수도 있습니다. 예를 들어 OpenEntityManagerInViewFilter를 작동 시키기 위해.ContextLoaderListener 설정 다음은 당신의 DispatcherServlet을 함께 구성 :

<servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
</servlet>

contextConfigLocation 매개 변수 값이 비어 있는지 확인하십시오 .


Spring-MVC 애플리케이션에서 수행 한 작업을 공유하고 싶습니다.

  1. we-mvc-config.xml나는 @Controller로 주석 단지 클래스를 추가합니다.

    <context:component-scan base-package="com.shunra.vcat">
        <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>
    
  2. applicationContext.xml파일 나는 모든 나머지를 추가합니다.

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

참고 URL : https://stackoverflow.com/questions/9016122/contextloaderlistener-or-not

반응형