Results 1 to 3 of 3

Thread: How to avoid Spring bean conflicts with the same name

  1. #1
    Join Date
    Jun 2009
    Location
    London
    Posts
    6

    Question 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. #2
    Join Date
    Apr 2008
    Location
    Philadelphia, US
    Posts
    198

    Arrow

    @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
    Humans are stateful and mutable beings that have no problems processing many things concurrently and share state with others + they are usually "coupled"

  3. #3
    Join Date
    Jun 2009
    Location
    London
    Posts
    6

    Default

    @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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •