【问题标题】:Does Spring Supports Interface InjectionSpring是否支持接口注入
【发布时间】:2014-05-20 14:25:55
【问题描述】:

Spring 是否支持Interface Injection completely

从“Martin Fowler”博客中提到,依赖于 bean 的组件需要实现为注入 BeanInterface

当我们考虑这种情况时,BeanNameAwareXXXAware Interfaces 可能会注入适当的 bean 对象。

除了 spring 提供的类之外,是否可以使用 Interface Injection 注入 userdefinedd Bean。

我对以上所有内容感到困惑。

【问题讨论】:

    标签: spring dependency-injection inversion-of-control


    【解决方案1】:

    这是正确和规范的概念。

    您依赖合约(接口)并注入它们,但容器负责它们的实现:

    public interface MyService {}
    
    public class MyServiceImpl implements MyService {}
    
    <bean id="myService" class="com.my.proj.MyServiceImpl"/>
    
    public class MyController {
    
       @Autowired
       private MyService service;
    
    }
    

    不要混合使用容器组件及其 API (*Aware) 和用户定义的应用程序组件。

    【讨论】:

      猜你喜欢
      • 2011-02-19
      • 2019-03-03
      • 2011-11-23
      • 1970-01-01
      • 2016-04-17
      • 2011-07-02
      • 1970-01-01
      • 2011-03-09
      • 2023-01-04
      相关资源
      最近更新 更多