Spring:为什么我们自动装配接口而不是实现的类? [英] Spring: Why do we autowire the interface and not the implemented class?

查看:68
本文介绍了Spring:为什么我们自动装配接口而不是实现的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例

interface IA
{
  public void someFunction();
}

@Resource(name="b")
class B implements IA
{
  public void someFunction()
  {
    //busy code block
  }
  public void someBfunc()
  {
     //doing b things
  }
}

@Resource(name="c")
class C implements IA
{
  public void someFunction()
  {
    //busy code block
  }
  public void someCfunc()
  {
     //doing C things
  }
}

class MyRunner
{

  @Autowire
  @Qualifier("b") 
  IA worker;

  worker.someFunction();
}

谁能给我解释一下.

  • spring 如何知道使用哪种多态类型.
  • 我需要@Qualifier 还是@Resource?
  • 为什么我们自动装配接口而不是实现的类?

推荐答案

spring 如何知道使用哪种多态类型.

How does spring know which polymorphic type to use.

只要接口只有一个实现,并且该实现用 @Component 注释并启用 Spring 的组件扫描,Spring 框架就可以找到(接口,实现)对.如果未启用组件扫描,则必须在 application-config.xml(或等效的 spring 配置文件)中明确定义 bean.

As long as there is only a single implementation of the interface and that implementation is annotated with @Component with Spring's component scan enabled, Spring framework can find out the (interface, implementation) pair. If component scan is not enabled, then you have to define the bean explicitly in your application-config.xml (or equivalent spring configuration file).

我需要@Qualifier 还是@Resource?

Do I need @Qualifier or @Resource?

一旦您有多个实现,那么您需要对每个实现进行限定,并且在自动装配期间,您需要使用 @Qualifier 注释来注入正确的实现,以及 <代码>@Autowired 注释.如果您使用@Resource(J2EE 语义),那么您应该使用此注释的 name 属性指定 bean 名称.

Once you have more than one implementation, then you need to qualify each of them and during auto-wiring, you would need to use the @Qualifier annotation to inject the right implementation, along with @Autowired annotation. If you are using @Resource (J2EE semantics), then you should specify the bean name using the name attribute of this annotation.

为什么我们自动装配接口而不是实现的类?

Why do we autowire the interface and not the implemented class?

首先,通常对接口进行编码始终是一个好习惯.其次,在 spring 的情况下,您可以在运行时注入任何实现.一个典型的用例是在测试阶段注入模拟实现.

Firstly, it is always a good practice to code to interfaces in general. Secondly, in case of spring, you can inject any implementation at runtime. A typical use case is to inject mock implementation during testing stage.

interface IA
{
  public void someFunction();
}


class B implements IA
{
  public void someFunction()
  {
    //busy code block
  }
  public void someBfunc()
  {
     //doing b things
  }
}


class C implements IA
{
  public void someFunction()
  {
    //busy code block
  }
  public void someCfunc()
  {
     //doing C things
  }
}

class MyRunner
{
     @Autowire
     @Qualifier("b") 
     IA worker;

     ....
     worker.someFunction();
}

您的 bean 配置应如下所示:

Your bean configuration should look like this:

<bean id="b" class="B" />
<bean id="c" class="C" />
<bean id="runner" class="MyRunner" />

或者,如果您在存在这些的包上启用了组件扫描,那么您应该使用 @Component 限定每个类,如下所示:

Alternatively, if you enabled component scan on the package where these are present, then you should qualify each class with @Component as follows:

interface IA
{
  public void someFunction();
}

@Component(value="b")
class B implements IA
{
  public void someFunction()
  {
    //busy code block
  }
  public void someBfunc()
  {
     //doing b things
  }
}


@Component(value="c")
class C implements IA
{
  public void someFunction()
  {
    //busy code block
  }
  public void someCfunc()
  {
     //doing C things
  }
}

@Component    
class MyRunner
{
     @Autowire
     @Qualifier("b") 
     IA worker;

     ....
     worker.someFunction();
}

然后 MyRunner 中的 worker 将被注入一个 B 类型的实例.

Then worker in MyRunner will be injected with an instance of type B.

这篇关于Spring:为什么我们自动装配接口而不是实现的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆