为什么Controller层注入的是Service接口,而不是ServiceImpl实现类
转自csdn--https://blog.csdn.net/weixin_39565597/article/details/78078728
今天看代码发现,写法和自己理解的java写法不一致,就查找资料,感觉这篇文章跟自己的理解一致,转发过来,以后查看方便。以下是原文转载,后面的评论有的也转过来了,实现类个人一般是用impl,作者用的impt。
今天在用maven+ssm时,发现一直提示报错:
Error creating bean with name ‘自己定义的实现类’: Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field
经过仔细排查,发现在注入service的时候写成
Service层
接口
interface PCI{}
接口实现类
@Service
class PCIImpt imeplements PCI{}
Controller层
@Autowired
private PCIImpt pciImpt; //注入了实现类
1
2
3
4
5
6
7
8
9
10
在没有使用maven管理的时候注入实现类编译一直未报错,也就是说编译可以通过,但是使用maven管理后,编译报错。
而正确的写法:
Controller层
@Autowired
private PCI pci; //注入接口以Autowired自动接收
结果:编译通过;
1
2
3
4
结论就是:注入的是实现类对象,接收的接口;理解为多态;(Controller–Service–ServiceImpt–Mapper)
要遵循Controller–Service接口–ServiceImpt实现类–Mapper接口模式
那么在Service接口有多个ServiceImpt实现类的情况,就需要指定参数名来选择哪个ServiceImpt实现类了。
Service层(此时有两个接口实现类)
@Service("PCIImpt1")
class PCIImpt1 imeplements PCI{}
@Service("PCIimpt2")
class PCIImpt2 imeplements PCI{}
Controller层
@Resource(name="PCIimpt2") //填PCIimpt1,注入PCIimpt1实现类,填PCIimpt2,则注入PCIimpt2实现类
private PCI pci; //注入接口以Resource手动指定接收
1
2
3
4
5
6
7
8
9
10
同理:在ServiceImpt实现类中也是通过Mapper接口来接收;即:
ServiceImpt实现类
@Autowired
private Mapper mapper;
1
2
3
————————————————
版权声明:本文为CSDN博主「猪爷爷先生」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_39565597/article/details/78078728
评论:
①由于在Service层使用了@Service注解,致使Spring在启动时会自动扫描并把PCIImpt实现类添加到Bean容器中(在容器中该Bean的实例名称为pCIImpt,第一个字母小写);②在Controller层里使用@Autowired注解,默认是使用byType在Bean容器瓶中查找出最适合的类的实例名赋值给pci(注意是按类来查找最适合的),在Bean容器瓶中最适合pci的Bean实例名称自然就是pCIImpt(因为它是PCI的接口实现类)。另外,接口的那个地方,也没放注解,它也没有实例化到Bean容器中,对于pci最适合的就只有pCIImpt
Mapper是跟Mybatis挂钩的interface,ServiceImpl使用这个interface进行CURD的操作。Controller使用的Service的Interface,实际上到现在SpringBoot框架,启动项含有自动扫描以后,不太需要Service上再加一个接口。如果是为了所谓的面向接口,其实也大可不必。因为实际使用场景中,我们的Service接口,基本不存在一个接口多个实现类这种场景。
浙公网安备 33010602011771号