programing

봄에 다른 xml 파일의 빈을 참조하는 방법

bestprogram 2023. 9. 24. 13:01

봄에 다른 xml 파일의 빈을 참조하는 방법

저는 xml 파일에 스프링 빈이 정의되어 있습니다.다른 xml 파일에서 참조하고 싶습니다.어떻게 해야 하죠?

몇 가지 옵션이 있습니다.

수입품

<import resource="classpath:config/spring/that-other-xml-conf.xml"/>

<bean id="yourCoolBean" class="org.jdong.MyCoolBean">
    <property name="anotherBean" ref="thatOtherBean"/>
</bean>


에 포함ApplicationContext시공

두 파일을 모두 사용자의 일부로 만듭니다.ApplicationContext= > 를 생성하면 가져올 필요가 없습니다.

예를 들어 테스트 중에 필요한 경우:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:META-INF/conf/spring/this-xml-conf.xml",
                    "classpath:META-INF/conf/spring/that-other-xml-conf.xml" })
public class CleverMoneyMakingBusinessServiceIntegrationTest {...}

웹 앱일 경우에는.web.xml:

<context-param> 
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/conf/spring/this-xml-conf.xml</param-value>
    <param-value>WEB-INF/conf/spring/that-other-xml-conf.xml</param-value>
</context-param>

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

독립형 앱, 라이브러리 등이라면..당신은 당신의 짐을 싣게 될 것입니다.ApplicationContext다음과 같이:

new ClassPathXmlApplicationContext( 
    new String[] { "classpath:META-INF/conf/spring/this-xml-conf.xml",
                   "classpath:META-INF/conf/spring/that-other-xml-conf.xml" } );

빈을 정의하는 xml을 가져오기만 하면 됩니다.<import resource="otherXml.xml">콩의 정의를 사용할 수 있을 겁니다.

사용가능classpath:에서resource특성:

<import resource="classpath:anotherXXML.xml" />

"3.18" 참조.스프링 참조의 이에 있는 "한 파일에서 다른 파일로 빈 정의 가져오기"

동일한 XML 파일의 빈을 참조하는 것과 동일하게 참조합니다.스프링 컨텍스트가 여러 XML 파일로 구성된 경우 모든 빈은 동일한 컨텍스트의 일부이므로 고유한 네임스페이스를 공유합니다.

또는 단일 xml 파일의 크기가 커지는 것을 방지하기 위해 여러 파일로 빈을 리팩토링하는 경우에는 현재 폴더에서 참조하기만 하면 됩니다.

<import resource="processors/processor-beans.xml"/>

코드에 여러 Spring bean 구성 파일을 로드하여 이 문제를 해결할 수도 있습니다.

ApplicationContext context = new ClassPathXmlApplicationContext(
    new String[] {"Spring-Common.xml", "Spring-Connection.xml","Spring-ModuleA.xml"});

모든 spring xml 파일을 project classpath 아래에 놓습니다.

project-classpath/Spring-Common.xml
project-classpath/Spring-Connection.xml
project-classpath/Spring-ModuleA.xml

그러나 위의 구현은 구성이 부족하고 오류가 발생하기 쉬우므로 모든 Springbean 구성 파일을 단일 XML 파일로 구성하는 것이 좋습니다.예를 들어, 다음을 만듭니다.Spring-All-Module.xml파일을 작성하고, 다음과 같이 전체 Spring bean 파일을 가져옵니다.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <import resource="common/Spring-Common.xml"/>
    <import resource="connection/Spring-Connection.xml"/>
    <import resource="moduleA/Spring-ModuleA.xml"/>

</beans>

이제 다음과 같은 단일 xml 파일을 로드할 수 있습니다.

ApplicationContext context = 
    new ClassPathXmlApplicationContext(Spring-All-Module.xml);

참고 Spring3에서 대체 솔루션은 JavaConfig @Import를 사용하는 것입니다.

톨리티우스가 제공한 답변은 매우 상세합니다.피터 뷰트코비치가 언급한 문제 때문에

저는 web.xml chunk에서 오류가 발생합니다.하나의 매개 변수 값이 제자리에서만 허용되는 것 같습니다.– 피터 부트코비치

저는 같은 문제를 만나서 같은 태그의 ","로 두 개의 경로를 나누어 해결했습니다.

이렇게 보일 겁니다.

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml,
                     /WEB-INF/daoContext.xml
        </param-value>
    </context-param>

언급URL : https://stackoverflow.com/questions/7711750/how-to-reference-a-bean-of-another-xml-file-in-spring