You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Load files in <import resource=... /> only once [SPR-1142] #5845
has: votes-jiraIssues migrated from JIRA with more than 10 votes at the time of importin: coreIssues in core modules (aop, beans, core, context, expression)status: bulk-closedAn outdated, unresolved issue that's closed in bulk as part of a cleaning processtype: enhancementA general enhancement
If I load now an application context which has "file1.xml" and "file2.xml" as config locations, the file "common-config.xml" will be loaded twice. I suggest to remember the url of already loaded config files.
Because this issue is opened for many years I was thinking of proposing a solution because our spring configuration uses lots of XML files decomposed with imports and lots of AOP configuration :) and computing unused bean definitions is expensive at runtime.
So here is a patch for loading application context resources only once against the trunk.
It contains:
modification to keep a list of previously imported resource URLs in order to know if this is an already imported resource (if this is the case it is ignored with an INFO log entry)
test cases
a paragraph in the reference documentation
This feature is enabled by setting the bean property importOnce to true on *XmlApplicationContext.
This property is false by default for backward compatibility purpose.
I do not know if this is interesting but I do not modified JavaConfig classes to reflect such behavior with @Import.
Let me know if this approach is right or if I need to go in another direction.
This behaviour is espacially then problematic, because BPP are executed multiple times if they are declared as anonymous beans or defined via a namespace eg. <context:property-placeholder ... . For the latter it is impossible to define an id. Since there are special beans that can be defined only once eg. the multipartresolver this problem means you cannot hide the multipartresolver in a xml file hierarchy as it may be loaded twice.
This is a real pain and punishes people who modularize their application context in multiple smaller XML files.
Please consider fixing this issue in 3.1 instead of 3.2.
Can someone provide more info on why you would import the same file more than once? I'm curious what is trying to be accomplished where this comes up, as I've never encountered this myself in practice.
It typically happens indirectly:
A imports B imports C
A imports D imports C
It's an old problem, in C the #ifndef directive was used in similar situations.
I have opened a related issue #12263, which I think would make the log messages a lot easier
to interpret in these situations.
Some additional thoughts to the comment of Dale Wyttenbach in answer to Keith Donald:
In order to ensure, that C isn't loaded twice we import in the topmost application.spring.xml all configurations (A, B, C and D). So the import of C is done only there, nowhere else.
Problem solved but other problems arise with this approach:
Design-flaw: dependencies between configurations aren't expressed explicitly
Running Tests: In order to run tests concerning for example B we had to load the configuration of B and C. Assumed C gets a dependency to a new configuration-file E, ALL tests of A,B,C AND D had to be changed. Instead we load the complete application-context for our tests. But now D and A are unnecessarily loaded for our B-test -> turnaround time increases.
Maybe there are approaches better than the one I outlined (interested to hear of them), but this issue would be handy to solve the problem in a simple way:
dependencies can be expressed explicitly (e.g. B imports C)
Each test had to load only the configuration of it's own module - dependent configurations are loaded transparently
Since this issue had been logged around 9 years back and is still unresolved, I was just wondering if there is some major reason for it?
We were thinking of including our own classes in our project to handle this problem, so wanted to know if there is possibility of some side-effects, before moving the code to production?
changed the title [-]Load files in <import resource=.../> only once [SPR-1142][/-][+]Load files in `<import resource=... />` only once [SPR-1142][/+]on Feb 21, 2023
has: votes-jiraIssues migrated from JIRA with more than 10 votes at the time of importin: coreIssues in core modules (aop, beans, core, context, expression)status: bulk-closedAn outdated, unresolved issue that's closed in bulk as part of a cleaning processtype: enhancementA general enhancement
Activity
spring-projects-issues commentedon Jan 5, 2010
Sébastien Launay commented
Because this issue is opened for many years I was thinking of proposing a solution because our spring configuration uses lots of XML files decomposed with imports and lots of AOP configuration :) and computing unused bean definitions is expensive at runtime.
So here is a patch for loading application context resources only once against the trunk.
It contains:
This feature is enabled by setting the bean property
importOncetotrueon*XmlApplicationContext.This property is
falseby default for backward compatibility purpose.I do not know if this is interesting but I do not modified JavaConfig classes to reflect such behavior with
@Import.Let me know if this approach is right or if I need to go in another direction.
spring-projects-issues commentedon Aug 12, 2010
Tim Ducheyne commented
Just a small remark about the patch:
I think it would be better to use URIs instead of URLs
There is a problem with the hashcode and equals implementation of URL
http://michaelscharf.blogspot.com/2006/11/javaneturlequals-and-hashcode-make.html
brgds,
Tim
spring-projects-issues commentedon Sep 30, 2010
Felix Simmendinger commented
This behaviour is espacially then problematic, because BPP are executed multiple times if they are declared as anonymous beans or defined via a namespace eg. <context:property-placeholder ... . For the latter it is impossible to define an id. Since there are special beans that can be defined only once eg. the multipartresolver this problem means you cannot hide the multipartresolver in a xml file hierarchy as it may be loaded twice.
spring-projects-issues commentedon Feb 17, 2011
Dennis Homann commented
This is a real pain and punishes people who modularize their application context in multiple smaller XML files.
Please consider fixing this issue in 3.1 instead of 3.2.
spring-projects-issues commentedon Feb 19, 2011
Keith Donald commented
Can someone provide more info on why you would import the same file more than once? I'm curious what is trying to be accomplished where this comes up, as I've never encountered this myself in practice.
spring-projects-issues commentedon Feb 19, 2011
Dale Wyttenbach commented
It typically happens indirectly:
A imports B imports C
A imports D imports C
It's an old problem, in C the #ifndef directive was used in similar situations.
I have opened a related issue #12263, which I think would make the log messages a lot easier
to interpret in these situations.
spring-projects-issues commentedon Mar 26, 2011
Andy Schäfer commented
Some additional thoughts to the comment of Dale Wyttenbach in answer to Keith Donald:
In order to ensure, that C isn't loaded twice we import in the topmost application.spring.xml all configurations (A, B, C and D). So the import of C is done only there, nowhere else.
Problem solved but other problems arise with this approach:
Design-flaw: dependencies between configurations aren't expressed explicitly
Running Tests: In order to run tests concerning for example B we had to load the configuration of B and C. Assumed C gets a dependency to a new configuration-file E, ALL tests of A,B,C AND D had to be changed. Instead we load the complete application-context for our tests. But now D and A are unnecessarily loaded for our B-test -> turnaround time increases.
Maybe there are approaches better than the one I outlined (interested to hear of them), but this issue would be handy to solve the problem in a simple way:
spring-projects-issues commentedon Mar 4, 2014
Neha commented
Since this issue had been logged around 9 years back and is still unresolved, I was just wondering if there is some major reason for it?
We were thinking of including our own classes in our project to handle this problem, so wanted to know if there is possibility of some side-effects, before moving the code to production?
spring-projects-issues commentedon Jun 10, 2015
Lucian Yao commented
Please fix this issue. I was blamed by the peers by using the import in lower-level modules to save a lot of time in config and deploy.
spring-projects-issues commentedon Jan 12, 2019
Bulk closing outdated, unresolved issues. Please, reopen if still relevant.
[-]Load files in <import resource=.../> only once [SPR-1142][/-][+]Load files in `<import resource=... />` only once [SPR-1142][/+]sbrannen commentedon Feb 21, 2023
If you were following this issue, you may be interested in the following.
BeanDefinitionregistration whenallowBeanDefinitionOverridingisfalse#27978