Announcement Announcement Module
Collapse
No announcement yet.
Autowired not working properly (Doesn't crash on multiple beans definition) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowired not working properly (Doesn't crash on multiple beans definition)

    Hello,

    I have a weird question, which is the opposite of pretty much all the questions I saw on here :P

    Basically, I have an interface
    Code:
    public interface Manager {
        Object create();
    }
    with a subinterface
    Code:
    public interface SubManager {
        Object delete();
    }
    The main interface is implemented 3 times by different classes in this way
    Code:
    public class MainManager implements SubManager {
        public Object create() { ... }
        public Object deleted() { ... }
    }
    Code:
    public class SecondManager implements Manager {
        public Object create() { ... }
    }
    Code:
    public class ThirdManager implements Manager {
        public Object create() { ... }
    }
    All of the config is defined in XML (no annotations yet anywhere in those classes)
    Code:
    <bean id="mainManager" class = "com.manager.impl.MainManager">
    <bean id="secondManager" class = "com.manager.impl.SecondManager">
    <bean id="thirdManager" class = "com.manager.impl.ThirdManager">

    Then I created a new class
    Code:
    public DoSomething {
        @Autowired private Manager manager;
    
        ...
    }
    And ... everything works fine, it gets the mainManager like it's supposed to, but

    My question is : Why the hell does this work?

    Everywhere I've read, Spring says that if there is more than 1 bean defined, there will be an exception, every search I do on Google tells me the same

    Why is spring taking my first bean into account and ignoring the rest?

    PS: There is no primary bean defined in any case, not in XML nor in Spring annotations
    PS 2 : I've defined a second bean with @Resource(name="secondManager") to see if, maybe, it didn't load my beans correctly, but that bean works too, so it turns out all my managers are loaded :|
    PS 3 : I'm using Spring v3.2




    I know the fix to this (@Resource), but the fact that it doesn't follow the behavior it's supposed to scares me a lil bit
    Last edited by lumino6; Aug 20th, 2012, 07:21 AM.

  • #2
    From the code you posted, the MainManager, SecondManager and ThirdManager did *not* actually inherit your AbstractManager. Is this your typo? or it is the real code you were testing?

    Comment


    • #3
      Big typo, I didn't understand that class correctly at first ...

      Fixed my first post
      Last edited by lumino6; Aug 20th, 2012, 07:21 AM.

      Comment


      • #4
        For those interested, I figured out my problem

        @Autowired tries to wire both by type and by name
        If it finds multiple beans of the same type, it will act like @Resource and try to find a bean with the same id as the name of the property

        In my case, my property was called contractManager, and my 3 beans were "contractManager", "contractOrderManager", "contractProposalManager"

        After finding these 3 beans, before crashing, it tried matching the name, and since we had a match, it took the match and didn't crash as expected

        I haven't figured out how to force @Autowired to ignore names yet, and try to match only by type (argument autowire=BY_TYPE on @Bean doesn't work ...)

        Comment

        Working...
        X