78

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.

Vy Do
  • 46,709
  • 59
  • 215
  • 313
Chitresh
  • 3,022
  • 12
  • 35
  • 40
  • Future readers, in case you have a "I think what I wrote should work" issue, also see this bug, with a "status=declined". https://github.com/spring-projects/spring-framework/issues/16017 Just in case the URL ever eventually fails, the title of the bug post is " Import of an XML file from the root of a JAR file with wildcard classpath and wildcard path does not work [SPR-11390] " – granadaCoder Feb 24 '19 at 08:29

6 Answers6

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

Reference:

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
63

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: Divide Spring configuration across multiple projects

Spring classpath prefix difference

Community
  • 1
  • 1
RicardoS
  • 2,088
  • 1
  • 21
  • 22
10

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" />

yogesh
  • 101
  • 1
  • 2
7

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);
    }
}
Devrim
  • 15,345
  • 4
  • 66
  • 74
2
<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.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
JitenS
  • 41
  • 1
1

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>
ajeetkg
  • 91
  • 1
  • 2