ContextLoaderListener는?
표준 스프링 웹 애플리케이션(Roo 또는 "Spring MVC Project" 템플릿에서 작성)은 다음을 사용하여 web.xml을 만듭니다.ContextLoaderListener
그리고.DispatcherServlet
를 사용하여 설정 전체를 로드하지 않는 이유는 무엇입니까?
ContextLoaderListener는 웹에 관련되지 않은 것을 로드하기 위해 사용해야 하며 DispatcherServlet은 웹에 관련된 것(Controllers 등)을 로드하기 위해 사용해야 한다는 것을 이해합니다.그 결과 부모 콘텍스트와 자녀 콘텍스트의 두 가지 콘텍스트가 생성됩니다.
배경:
나는 몇 년 동안 이 표준적인 방법으로 그것을 하고 있었다.
<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>
이로 인해 종종 두 컨텍스트와 두 컨텍스트 간의 의존성에 문제가 발생하였습니다.이전에는 항상 솔루션을 찾을 수 있었습니다.이것에 의해서, 소프트웨어의 구조/아키텍처가 항상 개선되는 것을 강하게 느끼고 있습니다.그러나 지금 나는 두 문맥의 이벤트에 관한 문제에 직면해 있다.
-- 그러나, 이 2개의 콘텍스트 패턴을 다시 생각하게 되어, 왜 내가 이 문제에 휘말려야 하는지, 왜 스프링 구성 파일을 모두 1개로 로드하지 않는지 자문하고 있습니다.DispatcherServlet
및 삭제ContextLoaderListener
(다른 컨피규레이션파일은 가질 예정이지만 컨텍스트는 1개뿐입니다).
제거하지 않을 이유가 있습니까?ContextLoaderListener
?
당신 경우엔, 아니, 그 돈을 가지고 있어야 할 이유가 없어ContextLoaderListener
그리고.applicationContext.xml
서블릿 콘텍스트만으로 정상적으로 동작하는 앱이라면 더 간단합니다.
네, 일반적으로 장려되는 패턴은 웹 앱 수준의 컨텍스트에서 웹이 아닌 내용을 유지하는 것이지만, 이는 취약한 규칙에 지나지 않습니다.
웹 앱 수준의 컨텍스트를 사용해야 하는 유일한 이유는 다음과 같습니다.
- 여러 개를 가지고 있는 경우
DispatcherServlet
서비스를 공유할 필요가 있는 고객 - 스프링 배선 서비스에 액세스해야 하는 레거시/비 스프링 서블릿이 있는 경우
- 웹 앱 수준의 컨텍스트에 후크하는 서블릿 필터가 있는 경우(예: Spring Security's)
DelegatingFilterProxy
,OpenEntityManagerInViewFilter
등)
이들 중 어느 것도 해당되지 않으므로 추가 복잡성은 보증되지 않습니다.
예약된 작업, JMS 연결 등과 같은 백그라운드 작업을 서블릿 컨텍스트에 추가할 때는 주의하십시오.추가하는 것을 잊은 경우<load-on-startup>
고객님께web.xml
servlet에 처음 액세스할 때까지 이러한 작업은 시작되지 않습니다.
응용 프로그램콘텍스트를 반대로 설정할 수도 있습니다.예를 들어 OpenEntityManager를 만들기 위해InViewFilter 동작.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 어플리케이션에서 수행한 작업을 공유합니다.
에서
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>
에서
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
'itsource' 카테고리의 다른 글
단말기 내에서 mysql을 사용하여 경고 메시지를 표시하지 않지만 비밀번호는 bash 스크립트로 작성됩니다. (0) | 2022.10.25 |
---|---|
PHP - php 파일 포함 및 쿼리 매개 변수 전송 (0) | 2022.10.25 |
운영 체제 정보 가져오기 (0) | 2022.10.25 |
Python 스레드화에 join()을 사용하는 이유는 무엇입니까? (0) | 2022.10.25 |
PHP 버전을 PHPStorm으로 설정하려면 어떻게 해야 하나요? (0) | 2022.10.25 |