一、当有一个实现类时@Service

@Service    //将该service注入到容器
public class TestDemoImp implements TestDemo{
    //

}
@RestController    //控制器注解
public class TestController{
   
    @AutoWired     //自动导入bean,按name导入
    private TestDemo testDemo;
}

 

二、当有多个实现类时@Service(" name ")

@Service("testDemo")
public class TestDemoImp implements TestDemo{
    //

}
@RestController
public class TestController{
   
    @AutoWired
    private TestDemo testDemo;//此变量名与@Service(" ")相同
}

 

三、当有多个实现类时@Resource("name")

@Service("testDemo")
public class TestDemoImp implements TestDemo{
    //

}
@RestController
public class TestController{
   
    @Resource("name=testDemo")//此name值与@Service(" ")相同
    private TestDemo testDemo;
}

四、@Resource和@Autowire的区别

此项可以参考@Resource与@Autowire注解的区别,此文十分清晰的解释了两注解的关系。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐