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

查看:118
本文介绍了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();
}

有人可以向我解释一下。

Can someone explain this to me.


  • 春季如何知道要使用的多态类型。

  • 我需要 @Qualifier @Resource

  • 为什么我们自动连接界面而不是实现的类?

  • How does spring know which polymorphic type to use.
  • Do I need @Qualifier or @Resource?
  • Why do we autowire the interface and not the implemented class?

推荐答案


春季如何知道要使用的多态类型。

How does spring know which polymorphic type to use.

只要有只有单一的接口实现,并且使用Spring的组件扫描启用了 @Component 注释实现,Spring框架可以找出(接口,实现)对。如果未启用组件扫描,则必须在application-config.xml(或等效的弹簧配置文件)中显式定义该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语义),则应使用此注释的名称属性指定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();
}

然后 worker MyRunner 将注入一个类型为 B 的实例。

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

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

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