广告
返回顶部
首页 > 资讯 > 后端开发 > Python >聊聊@Autowired注解注入,写接口名字还是实现类的名字
  • 186
分享到

聊聊@Autowired注解注入,写接口名字还是实现类的名字

2024-04-02 19:04:59 186人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录用@Autowired注解注入,写接口名字还是实现类的名字来自某程序员一个问答问题spring中Autowired注入接口的几个问题1.Spring怎么知道注入哪个实现?2.需要

用@Autowired注解注入,写接口名字还是实现类的名字

来自某程序员一个问答问题

1. 有一点没明白,为什么注解@repository注解的是接口UserDAO的实现类UserDAOImpl,而在UserServiceImpl中使用@Autowired注解注入属性private UserDAO userDAO自动装配,为什么最后得到的是UserDAOImpl的实例。

-----上面是某位同学的提问,我也有这样的疑问----

2. @Service注解服务层的时候,在unitest中,是从ApplicationContext.getBean("实现类名字,首字母小写") 这样获取的。

也就是说,在容器中初始化的Bean应该按照实现类名字规则。 这一点如果是用xml配置是不存在这样的问题,因为xml可以指定id, id 是接口,class指向实现类。

3. 来自网友的回答:这个其实是创建了实现类的对象但引用了接口类型,即"InjectionDao injectionDao = new InjectionDaoImpl()", 这个其实是Java多态性(向上转型)的一种应用。在实现类处加@Repository注解,意思就是new InjectionDaoImpl(), 而在InjectionServiceImpl中定义属性InjectionDAO injectionDAO就是将new出来的这个InjectionDaoImpl对象向上转型为InjectionDao类型。

Spring中Autowired注入接口的几个问题

1.Spring怎么知道注入哪个实现?

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).

如果Spring配置了component scan,并且要注入的接口只有一个实现的话,那么spring框架可以自动将interface于实现组装起来。如果没有配置component scan,那么你必须在application-config.xml(或等同的配置文件)定义这个bean。

可以理解为 在 application-service.xml 配置文件中声明了 component-scan


<context:component-scan base-package="com.system.service.impl"/>

在 com.system.service 下有一个接口类,在 com.system.service.impl 下有一个接口对应的实现类,且该实现类用 @Service 注解进行了标注。

在使用该接口类的时候,可以按照如下方式:


@Autowired
private UserloginService userloginService;

以此来进行自动装配。

2.需要@Qualifier和@Resource注解吗?

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.

一旦一个接口有多个实现,那么就需要每个特殊化识别并且在自动装载过程中使用@Qualifier和@Autowired一起使用来标明。如果是使用@Resource注解,那么你应该使用resource中属性名称来标注@Autowired.

3.为什么@Autowired使用在interface上而不是实现类上?

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.

首先,一般使用接口是很常用并且有益的变成技术。

其次,在spring中,你可以在运行过程中注入各种实现。一个很经典的情况就是在测试阶段,注入模拟的实现类。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 聊聊@Autowired注解注入,写接口名字还是实现类的名字

本文链接: https://www.lsjlt.com/news/157665.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作