使用@Autowired时如何注入不同的实现类

时间:2016-05-02 04:18:01

标签: java spring

我有以下结构:

public interface MyService{
  public void someMethod();
}

@Service
public class MyServiceImplA implements MyService {
  public void someMethod() {
       return;
}

在我的控制器中,我这样做:

@Autowired
MyServiceImplA myService;

   myService.somemethod();

我不确定我应该如何在这里使用界面?如果我有第二个实现,比如MyServiceImplB,会发生什么?我可以通过某种配置(我正在使用Java Config)来设置它吗

1 个答案:

答案 0 :(得分:5)

@Autowired
@Qualifier("fooService")
MyService myService;

<form id="promacform" onsubmit="return protein_calculator(this);" method="post">
    Weight: <input type="text" name="protein_weight" id="protein_weight" size="5"; /> lbs.<br />
    Activity level (1.1-1.4): <input type="text" name="protein_activity" id="protein_activity" size="5"; /> <br />
    <br><input type="submit" name="submit" id="submit" value="Calculate" /><br />
    <div id="protein_result"></div>
</form>
相关问题