applicationContext를 여러 파일로 분할하는 중
Spring의 구성을 여러 xml 파일로 분할하는 올바른 방법은 무엇입니까?
내가 가지고 있는 그 순간에
/WEB-INF/foo-servlet.xml/WEB-INF/foo-service.xml/WEB-INF/foo-persistence.xml
나의web.xml는 다음과 같습니다.
<servlet>
<description>Spring MVC Dispatcher Servlet</description>
<servlet-name>intrafest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/foo-*.xml
</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/foo-*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
실제 질문:
- 이 접근 방식이 올바른/최상입니까?
- 구성 위치를 반드시 지정해야 합니까?
DispatcherServlet그리고.context-param섹션?
다음에 정의된 콩을 참조할 수 있으려면 무엇을 염두에 두어야 합니까?foo-servlet.xml부터foo-service.xml? 이것이 다음을 지정하는 것과 관련이 있습니까?contextConfigLocation인에web.xml?
업데이트 1:
저는 Spring framework 3.0을 사용하고 있습니다.이렇게 리소스를 가져올 필요가 없다는 것을 수행할 필요가 없습니다.
<import resource="foo-services.xml"/>
이것이 정확한 가정입니까?
저는 다음 설정이 가장 쉽다고 생각합니다.
DispatcherServlet의 기본 구성 파일 로드 메커니즘을 사용합니다.
이 프레임워크는 Dispatcher Servlet을 초기화할 때 웹 응용 프로그램의 WEB-INF 디렉토리에서 [servlet-name]-servlet.xml이라는 이름의 파일을 찾고 여기서 정의된 콩을 만듭니다(글로벌 범위에서 동일한 이름으로 정의된 콩의 정의를 무시함).
당신의 경우, 파일을 만들기만 하면 됩니다.intrafest-servlet.xml에서WEB-INFdir 및 특정 정보를 지정할 필요가 없습니다.web.xml.
인intrafest-servlet.xml파일을 사용하여 XML 구성을 구성할 수 있습니다.
<beans>
<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
<import resource="foo-services.xml"/>
<import resource="foo-persistence.xml"/>
</beans>
Spring 팀은 실제로 (Web) ApplicationContext를 작성할 때 여러 구성 파일을 로드하는 것을 선호합니다.그래도 이런 식으로 하고 싶다면 두 컨텍스트 파라미터를 모두 지정할 필요는 없을 것 같습니다. (context-param) 및 서블릿 초기화 파라미터(init-param둘 중 하나면 됩니다.쉼표를 사용하여 여러 구성 위치를 지정할 수도 있습니다.
Mike Nereson은 자신의 블로그에 다음과 같이 말하고 있습니다.
http://blog.codehangover.com/load-multiple-contexts-into-spring/
이렇게 하는 데는 몇 가지 방법이 있습니다.
1. web.xml contextConfigLocation
첫 번째 옵션은 ContextConfigLocation 요소를 통해 웹 응용프로그램 컨텍스트에 모두 로드하는 것입니다.웹 응용프로그램을 작성하는 경우를 가정하여 기본 응용프로그램Context가 이미 여기에 있을 것입니다.다음 컨텍스트의 선언 사이에 공백만 두면 됩니다.
<context-param> <param-name> contextConfigLocation </param-name> <param-value> applicationContext1.xml applicationContext2.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>위의 내용은 운송료 반환을 사용합니다.아니면 그냥 공간을 넣을 수도 있습니다.
<context-param> <param-name> contextConfigLocation </param-name> <param-value> applicationContext1.xml applicationContext2.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>2. applicationContext.xml 리소스 가져오기
다른 옵션은 기본 applicationContext.xml을 web.xml에 추가한 다음 해당 기본 컨텍스트에서 가져오기 문을 사용하는 것입니다.
applicationContext.xml당신은 아마..<!-- hibernate configuration and mappings --> <import resource="applicationContext-hibernate.xml"/> <!-- ldap --> <import resource="applicationContext-ldap.xml"/> <!-- aspects --> <import resource="applicationContext-aspects.xml"/>어떤 전략을 사용해야 합니까?
1.저는 항상 web.xml을 통해 로딩하는 것을 선호합니다.
왜냐하면, 이것은 제가 모든 맥락을 서로 격리할 수 있게 해주기 때문입니다.테스트를 사용하면 테스트를 실행하는 데 필요한 컨텍스트만 로드할 수 있습니다.를 통해 요소가다로 됩니다.
loosely coupled인 수 있게2.컨텍스트를 에 로드하는 경우
non-web application, 나는 그것을 사용할 것입니다.import자원.
우리가 다루고 있는 컨텍스트는 두 가지 유형이 있습니다.
1: 루트 컨텍스트(부모 컨텍스트).일반적으로 모든 jdbc(ORM, 최대 절전 모드) 초기화 및 기타 스프링 보안 관련 구성 포함)
2: 개별 서블릿 컨텍스트(자녀 컨텍스트).일반적으로 Dispatcher Servlet Context 및 spring-mvc(컨트롤러, URL Mapping 등)와 관련된 모든 빈을 초기화합니다.
다음은 여러 응용프로그램 컨텍스트 파일을 포함하는 web.xml의 예입니다.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Spring Web Application example</display-name>
<!-- Configurations for the root application context (parent context) -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/jdbc/spring-jdbc.xml <!-- JDBC related context -->
/WEB-INF/spring/security/spring-security-context.xml <!-- Spring Security related context -->
</param-value>
</context-param>
<!-- Configurations for the DispatcherServlet application context (child context) -->
<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>
/WEB-INF/spring/mvc/spring-mvc-servlet.xml
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/admin/*</url-pattern>
</servlet-mapping>
</web-app>
@eljenso : intrafest-servlet.xml webapplication context xml은 응용 프로그램이 SPRING WEB MVC를 사용하는 경우 사용됩니다.
그렇지 않으면 @kosaant 구성이 문제가 없습니다.
SPRING WEB MVC를 사용하지 않고 SPRING IOC를 사용하려는 경우의 간단한 예:
web.xml에서:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
application-context.xml이 됩니다.<import resource="foo-services.xml"/>다양한 응용프로그램 컨텍스트 파일을 로드하고 주 응용프로그램- context.xml에 넣기 위한 문을 가져옵니다.
감사합니다. 도움이 되길 바랍니다.
저는 모듈러-스프링-컨텍스트의 저자입니다.
이 라이브러리는 Composing XML 기반 구성 메타데이터를 사용하여 달성하는 것보다 스프링 컨텍스트의 모듈식 구성을 가능하게 하는 작은 유틸리티 라이브러리입니다.modular-spring-contexts는 기본적으로 독립 실행형 응용 프로그램 컨텍스트인 모듈을 정의하고 모듈이 원래 모듈에서 내보내는 다른 모듈에서 원두를 가져올 수 있도록 하는 방식으로 작동합니다.
그렇다면 핵심은.
- 모듈 간 종속성 제어
- 어떤 콩이 수출되고 어디에 사용되는지에 대한 통제
- 콩의 이름을 짓는 충돌 가능성 감소
간단한 예는 다음과 같습니다.
moduleDefinitions.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:module="http://www.gitlab.com/SpaceTrucker/modular-spring-contexts"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.gitlab.com/SpaceTrucker/modular-spring-contexts xsd/modular-spring-contexts.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<module:module id="serverModule">
<module:config location="/serverModule.xml" />
</module:module>
<module:module id="clientModule">
<module:config location="/clientModule.xml" />
<module:requires module="serverModule" />
</module:module>
</beans>
serverModule.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:module="http://www.gitlab.com/SpaceTrucker/modular-spring-contexts"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.gitlab.com/SpaceTrucker/modular-spring-contexts xsd/modular-spring-contexts.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean id="serverSingleton" class="java.math.BigDecimal" scope="singleton">
<constructor-arg index="0" value="123.45" />
<meta key="exported" value="true"/>
</bean>
</beans>
clientModule.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:module="http://www.gitlab.com/SpaceTrucker/modular-spring-contexts"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.gitlab.com/SpaceTrucker/modular-spring-contexts xsd/modular-spring-contexts.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<module:import id="importedSingleton" sourceModule="serverModule" sourceBean="serverSingleton" />
</beans>
언급URL : https://stackoverflow.com/questions/600095/splitting-applicationcontext-to-multiple-files
'programing' 카테고리의 다른 글
| 하위 쿼리 결과에서 LIMIT 값 가져오기 (0) | 2023.10.09 |
|---|---|
| C#에서 JSON을 XML로 변환하거나 XML을 JSON으로 변환하는 방법은? (0) | 2023.10.09 |
| 단일 그룹 그룹 함수가 아님 (0) | 2023.10.09 |
| jQuery에서 AJAX 요청이 성공했는지 확인하는 가장 좋은 방법 (0) | 2023.10.09 |
| "에서 발견된 예기치 않은 요소" 오류를 수정하려면 어떻게 해야 합니까? (0) | 2023.10.09 |