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

JavaSpring

Java Problem Overview


I have two projects with the name simple-core-impl and simple-core-web.

Both projects are spring based and both have a parent project name simple-core.

I have simple-impl-config.xml in simple-core-impl project and simple-web-config.xml in simple-impl-config.xml.

I have a bean which has class: simple service which have one method which returns me a message "hello World".

I want to import the simple-impl-config.xml in the simple-web-config.xml so the bean is available into my controller which is in simple-core-web project.

simple-core-web project has a jar of simple-core-impl project.

So please tell me how I can I import spring-config.xml of one project into spring-config.xml of another project so all the beans of first is available into other project by just importing?

I do not want to rewrite all the beans.

Java Solutions


Solution 1 - Java

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

Reference:

Solution 2 - Java

A small variation of Sean's answer:

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

With the asterisk in order to spring search files 'spring-config.xml' anywhere in the classpath.

Another reference: https://stackoverflow.com/questions/5092133/divide-spring-configuration-across-multiple-projects

https://stackoverflow.com/questions/3294423/spring-classpath-prefix-difference

Solution 3 - Java

For some reason, import as suggested by Ricardo didnt work for me. I got it working with following statement:

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

Solution 4 - Java

Here is the annotation based example:

@SpringBootApplication
@ImportResource({"classpath*:spring-config.xml"})
public class MainApplication {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

Solution 5 - Java

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

This is the most suitable one for class path configuration. Particularly when you are searching for the .xml files in a different project which is in your class path.

Solution 6 - Java

You have to add the jar/war of the module B in the module A and add the classpath in your new spring-module file. Just add this line

spring-moduleA.xml - is a file in module A under the resource folder. By adding this line, it imports all the bean definition from module A to module B.

MODULE B/ spring-moduleB.xml


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

<bean id="helloBeanB" class="basic.HelloWorldB">
  <property name="name" value="BMVNPrj" />
</bean>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChitreshView Question on Stackoverflow
Solution 1 - JavaSean Patrick FloydView Answer on Stackoverflow
Solution 2 - JavaRicardoSView Answer on Stackoverflow
Solution 3 - JavayogeshView Answer on Stackoverflow
Solution 4 - JavaDevrimView Answer on Stackoverflow
Solution 5 - JavaJitenSView Answer on Stackoverflow
Solution 6 - JavaajeetkgView Answer on Stackoverflow