springboot中service的注入方式
一、当有一个实现类时@Service@Service//将该service注入到容器public class TestDemoImp implements TestDemo{//}@RestController//控制器注解public class TestController{@AutoWired//自动导入bean,按name导入private TestDemo testDemo;}二、当有多
·
一、当有一个实现类时@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注解的区别,此文十分清晰的解释了两注解的关系。
更多推荐
已为社区贡献1条内容
所有评论(0)