java - 为什么spring不支持接口注入
PHPz
PHPz 2017-04-18 09:36:46
0
1
780

ioc有三种注入方式:setter、构造、接口。为什么spring不支持接口方式注入啊?

搜到的答案,大概是:接口注入模式因为历史较为悠久,在很多容器中都已经得到应用。但由于其在灵活性、易用性上不如其他两种注入模式,因而在 IOC 的专题世界内并不被看好。

还有没有其他的原因呢?

PHPz
PHPz

学习是最好的投资!

reply all(1)
Ty80

Spring’s documentation says:

DI exists in two major variants, Constructor-based dependency
injection and Setter-based dependency injection.

My understanding is that interface injection is actually implemented through setter injection:

interface InjectPerson {
    public void injectHere(Person p);
}

class Company implements InjectPerson {
   Person injectedPerson; 

   public void injectHere(Person p) {
        this.injectedPerson = p;
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!