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
Multiple bean instances are created when no id is specified [SPR-11757] #16379
When importing the same context more then once and there is a bean defined without id it will be created (and defined) multiple times.
In big application it is common case since there are a lot of configuration files that depends on each other and it should be acceptable that the same config is imported more than once.
I get what you mean but the idea is that if you want to ensure the uniqueness of a bean definition, you have to provide an id for it. It's the same general idea if a bean definition needs to be overridden as the overriding mechanism works through the use of the id.
From the reference:
"You are not required to supply a name or id for a bean. If no name or id is supplied explicitly, the container generates a unique name for that bean. However, if you want to refer to that bean by name, through the use of the ref element or Service Locator style lookup, you must provide a name. Motivations for not supplying a name are related to using inner beans and autowiring collaborators."
If so I must supply the id only if I want to refer it within the context.
I still insist it is a real problem and very non intuitive.
What I showed in my case is not multiple contexts but multiple configuration files that must be merged by Spring to a single context. Which is done and works fine except this specific case.
Moreover when we define PropertiesPlaceholderConfigurer it usually done without providing an id so multiple unnecessary beans will be created and work.
If so I must supply the id only if I want to refer it within the context.
I still insist it is a real problem and very non intuitive.
What I showed in my case is not multiple contexts but multiple configuration files that must be merged by Spring to a single context. Which is done and works fine except this specific case.
Moreover when we define PropertiesPlaceholderConfigurer it usually done without providing an id so multiple unnecessary beans will be created and work.
This is unfortunately a fundamental limitation at this point: We don't check resource imports for duplicates, since this is rather non-trivial in practice. See #14160 and further related issues.
The effect that you're seeing extra beans created for unnamed bean definitions naturally comes with those repeated imports. One could argue that the id generation is the problem there but we're not making any guarantees about unique ids for multiple bean definition registration attempts; we just keep the id unique for each registered bean definition.
From that perspective, the root of all evil is the fact that imports are processed twice, so we'll consider your issue along with the general enhancement in #14160.
Activity
spring-projects-issues commentedon May 6, 2014
Stéphane Nicoll commented
I get what you mean but the idea is that if you want to ensure the uniqueness of a bean definition, you have to provide an id for it. It's the same general idea if a bean definition needs to be overridden as the overriding mechanism works through the use of the id.
When you don't specify any id at all, one unique is generated for you so if the same context is loaded multiple times, a bean with no id will be declared multiple times as well. Check the doc for more details:
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-definition
spring-projects-issues commentedon May 6, 2014
Victor Perepelitsky commented
From the reference:
"You are not required to supply a name or id for a bean. If no name or id is supplied explicitly, the container generates a unique name for that bean. However, if you want to refer to that bean by name, through the use of the ref element or Service Locator style lookup, you must provide a name. Motivations for not supplying a name are related to using inner beans and autowiring collaborators."
If so I must supply the id only if I want to refer it within the context.
I still insist it is a real problem and very non intuitive.
What I showed in my case is not multiple contexts but multiple configuration files that must be merged by Spring to a single context. Which is done and works fine except this specific case.
Moreover when we define PropertiesPlaceholderConfigurer it usually done without providing an id so multiple unnecessary beans will be created and work.
spring-projects-issues commentedon May 6, 2014
Victor Perepelitsky commented
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-definition
From the reference:
"You are not required to supply a name or id for a bean. If no name or id is supplied explicitly, the container generates a unique name for that bean. However, if you want to refer to that bean by name, through the use of the ref element or Service Locator style lookup, you must provide a name. Motivations for not supplying a name are related to using inner beans and autowiring collaborators."
If so I must supply the id only if I want to refer it within the context.
I still insist it is a real problem and very non intuitive.
What I showed in my case is not multiple contexts but multiple configuration files that must be merged by Spring to a single context. Which is done and works fine except this specific case.
Moreover when we define PropertiesPlaceholderConfigurer it usually done without providing an id so multiple unnecessary beans will be created and work.
spring-projects-issues commentedon May 6, 2014
Juergen Hoeller commented
This is unfortunately a fundamental limitation at this point: We don't check resource imports for duplicates, since this is rather non-trivial in practice. See #14160 and further related issues.
The effect that you're seeing extra beans created for unnamed bean definitions naturally comes with those repeated imports. One could argue that the id generation is the problem there but we're not making any guarantees about unique ids for multiple bean definition registration attempts; we just keep the id unique for each registered bean definition.
From that perspective, the root of all evil is the fact that imports are processed twice, so we'll consider your issue along with the general enhancement in #14160.
Juergen
spring-projects-issues commentedon May 7, 2014
Victor Perepelitsky commented
Thanks for quick response.
Are you planning to fix #14160? When?