Results 1 to 4 of 4

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

Threaded View

  1. #1
    Join Date
    Jun 2012
    Posts
    6

    Default 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 at 07:21 AM.

Posting Permissions

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