Search code examples
javaspringdependency-injectioninversion-of-controlautowired

Spring 3.2 Default for @Autowired


What is the default autowire method for Spring 3.2? so let's say I have this class

public class Saxophonist implements IPerform{

    @Override
    public String perform() {
        return "I am A Saxophonist and I am Playing The : "+ saxophone;
    }

    private Instrument saxophone;

    public Instrument getSaxophone() {
        return saxophone;
    }

    @Autowired
    public void setSaxophone(Instrument saxophone) {
        this.saxophone = saxophone;
    }
}

Will it perform byName or will it perform byType?


Solution

  • @Autowired is type-driven but it provides a fallback catch to inject bean by name. Use @Resource if you want to perform a bean lookup and injection based on name, alternatively @Autowired along with @Qualifier can work as well, even though it is more useful when grouping beans. http://www.spiritwalker-jiang.com/archive/2013/02/11/Understanding-Autowired-annation-in-Spring