ApplicationContext를 여러 파일로 분할합니다
-
03-07-2019 - |
문제
스프링 구성을 여러 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
?
Update 1:
사용 중입니다 봄 프레임 워크 3.0. 다음과 같이 자원 가져올 필요가 없다는 것은 내 이해입니다.
<import resource="foo-services.xml"/>
이것이 올바른 가정입니까?
해결책
다음 설정이 가장 쉬운 것을 발견했습니다.
기본 구성 파일로드 메커니즘을 사용하십시오 Dispatcherservlet:
Framework는 Dispatcherservlet의 초기화시 웹 애플리케이션의 Web-Inf 디렉토리에서 [servlet-name] -servlet.xml이라는 파일을 찾아서 정의 된 Bean을 만듭니다 ( 글로벌 범위에서 같은 이름).
귀하의 경우 파일을 만듭니다 intrafest-servlet.xml
에서 WEB-INF
DIR 및 특정 정보를 지정할 필요가 없습니다. 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>
스프링 팀은 실제로 (웹) ApplicationContext를 작성할 때 여러 구성 파일을로드하는 것을 선호합니다. 여전히 이런 식으로하고 싶다면 두 컨텍스트 매개 변수를 지정할 필요가 없다고 생각합니다 (context-param
) 그리고 서블릿 초기화 매개 변수 (init-param
). 둘 중 하나가 할 것입니다. 쉼표를 사용하여 여러 구성 위치를 지정할 수도 있습니다.
다른 팁
Mike Nereson은 다음과 같은 블로그에서 다음과 같이 말합니다.
http://blog.codehangover.com/load-multiple-contexts-into-spring/
이 작업을 수행하는 몇 가지 방법이 있습니다.
1. Web.xml ContextConfigLocation
첫 번째 옵션은 ContextConfigLocation 요소를 통해 웹 응용 프로그램 컨텍스트에 모두로드하는 것입니다. 웹 애플리케이션을 작성한다고 가정하면 이미 기본 ApplicationContext가 여기에 있습니다. 다음 컨텍스트의 선언 사이에 공백을 두는 것입니다.
<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>
위의 경우 캐리지 리턴을 사용합니다. 또는 Yo는 공간에 넣을 수 있습니다.
<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. 컨텍스트를로드하는 경우 a
non-web application
, 나는 그것을 사용할 것이다import
자원.
우리가 다루는 맥락에는 두 가지 유형이 있습니다.
1: 루트 컨텍스트 (부모 컨텍스트. 일반적으로 모든 JDBC (ORM, Hibernate) 초기화 및 기타 스프링 보안 관련 구성 포함)
2: 개별 서블릿 컨텍스트 (아동 컨텍스트. 생식 적으로 파견자 서블릿 컨텍스트 및 스프링 -MVC와 관련된 모든 콩을 초기화합니다 (컨트롤러, URL 매핑 등).
다음은 여러 응용 프로그램 컨텍스트 파일을 포함하는 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 : 응용 프로그램이 Spring Web MVC를 사용하는 경우 intrafest-servlet.xml webApplication 컨텍스트 XML이 사용됩니다.
그렇지 않으면 @kosoant 구성은 괜찮습니다.
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에 넣습니다.
감사합니다. 이것이 도움이되기를 바랍니다.
나는 저자입니다 모듈 식 스프링-컨텍스트.
이것은 사용함으로써 달성되는 것보다 더 모듈 식 스프링 컨텍스트의 모듈 식 구성을 허용하는 작은 유틸리티 라이브러리입니다. 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>