How to Import Spring-Config.Xml of One Project into Spring-Config.Xml of Another Project

How to import spring-config.xml of one project into spring-config.xml of another project?

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

Reference:

  • Composing XML-based configuration
    metadata
  • Resources (here the classpath:
    part is explained)

Importing a spring config file via EL in spring XML

I am not sure, but it could work if you build the path completly in EL:

<import resource="#{'../core/'.concat(systemProperties['test.demo.enable']  eq 'true'?'XXXX':'YYYY').concat('/my-context.xml')}" />

It works in one of my spring security intercept URL definition, but i have not testet it with your case.

how to reference a bean of another xml file in spring

You have a couple of options:

Import

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

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


Include in the ApplicationContext Construction

Make both files a part of your ApplicationContext when you create it => then no import is needed.

For example if you need it during testing:

@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 {...}

In case it is a web app, you'd do it in 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>

If it is a stand alone app, library, etc.. you would load your ApplicationContext as:

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

How to import spring-context from one sub-module to another sub-module in maven project?

Simply add this to the child xml document

<import resource="classpath:second-config.xml" />

How to import Java-config class into XML-config so that both contexts have beans?

This actually ended up being fairly simple. To get a Java-config bean definition into the xml-config, simply define the Java-config class as a bean within the XML-config. There are no extra jars necessary.

@Configuration
public class SomeJavaConfig {

@bean
... [bean definition]
}

inside the XML-config, you define this class as a bean.

<!-- needed to pick up the annotated java-config -->
<context:annotation-config />

<!-- Importing java-config class, which are annotated with @Configuration -->
<bean name="SomeJavaConfig" class="[fully qualified path].SomeJavaConfig" />

The XML-config, which may be part of a different context, now has all the bean definitions defined within the JavaConfig class.

UPDATED - to included Alan Franzoni's comment below in the answer.



Related Topics



Leave a reply



Submit