Spirng about whether beans in different configuration files can be referenced to each other

Source: Internet
Author: User

After splitting into multiple, the bean's configuration can be placed in one of the files, and it does not have to be duplicated in each file.
Multiple files, such as applicationcontext-datasource.xml (data source), Applicationcontext-hibernate.xml, Applicationcontext-service.xml

You only need to configure in Web. xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/web-inf/applicationcontext-*.xml
</param-value>
</context-param>
This way, spring can load multiple files at the same time.

It can also be implemented in Struts-config.xml:

<plug-in
Classname= "Org.springframework.web.struts.ContextLoaderPlugIn" >
<set-property property= "Contextconfiglocation"
Value= "/web-inf/classes/spring-model-config.xml,/web-inf/classes/spring-web-config.xml"/>
</plug-in>

Load multiple configuration files as plug-ins.

In fact, they all have the same principle, as long as they have the desired bean in memory.


A bean in one configuration file wants to reference a bean in another file, you can use the
<property name= "DataSource" ><ref bean= "DataSource"/></property>
Get it like this. Attention is bean=, not local=.
If you are using local= to find beans from the current configuration file, use bean= if the bean is in a different file.

Summary: It is best to maintain a applicationcontext.xml original configuration file and import other configuration files in the original file, so that it is convenient to call all the beans, such as when the test can be context = new Classpathxmlapplicationcontext ("Applicationcontext.xml"); load all beans.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.