Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

spring

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.

like image 983
Chitresh Avatar asked Feb 25 '11 04:02

Chitresh


People also ask

How do I import one XML file into another in Spring?

You can use the ApplicationContext constructor to load bean definitions from multiple XML configuration files. This constructor takes an Array of strings representing the Resource locations. Or, like we see in this tutorial, you can also use the <import/> element to load bean definitions from other files.

Where is Spring XML configuration file?

In essence the Spring configuration files (that can have any name by the way, not just the generic applicationContext. xml ) are treated as classpath resources and filed under src/main/resources .

Can we use XML configuration in Spring boot?

Before Spring 3.0, XML was the only way to define and configure beans. Spring 3.0 introduced JavaConfig, allowing us to configure beans using Java classes. However, XML configuration files are still used today. In this tutorial, we'll discuss how to integrate XML configurations into Spring Boot.

How can you create an application context from multiple files which annotation can you used to combined multiple configuration files?

The @Import annotation is used to import multiple classes annotated with @Configuration. The @ImportResource annotation indicates one or more XML based bean definition files to be imported.


2 Answers

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

Reference:

like image 123
Sean Patrick Floyd Avatar answered Sep 22 '22 09:09

Sean Patrick Floyd


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

like image 37
RicardoS Avatar answered Sep 19 '22 09:09

RicardoS