I have a Spring bean defined in an xml file. I want to reference it from another xml file. How can I go about it?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
You reference it exactly as you would reference a bean in the same XML file. If a spring context is composed of several XML files, all the beans are part of the same context, and thus share a unique namespace.
You have a couple of options:
Import
Include in the
ApplicationContext
ConstructionMake both files a part of your
ApplicationContext
when you create it => then no import is needed.For example if you need it during testing:
In case it is a web app, you'd do it in
web.xml
:If it is a stand alone app, library, etc.. you would load your
ApplicationContext
as:Or if you are just refactoring beans into several files to keep the single xml file from growing to large, simply reference it from its current folder:
Just import the xml defining the bean with
<import resource="otherXml.xml">
and you will be able to use the bean definition.You can use
classpath:
in theresource
attribute:See the "3.18. Importing Bean Definitions from One File Into Another" in this chapter of the Spring Reference
You may also go about this by loading multiple Spring bean configuration files in the code :
Put all spring xml files under project classpath:
However, the above implementation is a lack of organizing and error prone, the better way should be organized all your Spring bean configuration files into a single XML file. For example, create a
Spring-All-Module.xml
file, and import the entire Spring bean files like this :Now you can load a single xml file like this :
Note In Spring3, the alternative solution is using JavaConfig @Import.