Announcement Announcement Module
Collapse
No announcement yet.
How to avoid Spring bean conflicts with the same name Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to avoid Spring bean conflicts with the same name

    Hello,
    Can anyone advise on the best practice for avoiding bean conflicts with beans of the same name in the same application context?

    For instance I have a common persistence library, used across multiple projects, which has a 'userService' bean defined. In my web project I am using Spring Security, which also has a 'userService' bean defined as an authorities’ populator. Now when the application context starts up there is a conflict between these two beans and I get a ConversionNotSupportedException when the beans are attempted to be injected because, obviously, they are of different types.

    Of course I can rename one of the 'userService' beans to be different to the other one but I keep coming up against this problem and I wondered if there was another way to prevent this happening, other than renaming beans to make sure they're unique?

    How do other suppliers of Spring written libraries prevent this problem so that their spring beans dont conflict with any user spring beans they may decalre with the same name?

    Thanks in advance
    AndyB

  • #2
    @andypb,

    Besides having bean name prefixes ( e.g. "security:userService", "persistence:userService" ), you can take a look at setAllowBeanDefinitionOverriding

    Code:
    javadoc: Set whether it should be allowed to override bean definitions by registering a different definition with the same name, automatically replacing the former. If not, an exception will be thrown. Default is "true".
    In Spring 3.0 it actually works for all the imports as well, as described in this jira: Make it easier to disallow bean definition overriding

    /Anatoly

    Comment


    • #3
      @litius ok thanks, I think the name prefixes will work for me. I did consider something similar but thought I might be missing something clever which allows you to load multiple bean definitions with the same name in the same application context but in different xml files.

      Comment

      Working...
      X