当前位置: 首页 > news >正文

水煮鱼wordpressseo在线优化平台

水煮鱼wordpress,seo在线优化平台,网页制作素材可爱图片,做网站分pc端和移动端的吗目录 1. 概述 2. 痛点 3. Qualifier 4. Qualifier VS Primary 5. 通过名称来自动注入 1. 概述 今天带你了解一下 Spring 框架中的 Qualifier 注解,它解决了哪些问题,以及如何使用它。我们还将了解它与 Primary 注解的不同之处。更多的技术解析请访…

目录

1. 概述

2. 痛点

3. @Qualifier

4. @Qualifier VS @Primary

5. 通过名称来自动注入


1. 概述

今天带你了解一下 Spring 框架中的 @Qualifier 注解,它解决了哪些问题,以及如何使用它。我们还将了解它与 @Primary 注解的不同之处。更多的技术解析请访问 felord.cn

2. 痛点

使用 @Autowired 注解是 Spring 依赖注入的绝好方法。但是有些场景下仅仅靠这个注解不足以让Spring知道到底要注入哪个 bean
默认情况下,@Autowired 按类型装配 Spring Bean
如果容器中有多个相同类型的 bean,则框架将抛出 NoUniqueBeanDefinitionException, 以提示有多个满足条件的 bean 进行自动装配。程序无法正确做出判断使用哪一个,下面就是个鲜活的例子:

    @Component("fooFormatter")public class FooFormatter implements Formatter {public String format() {return "foo";}}@Component("barFormatter")public class BarFormatter implements Formatter {public String format() {return "bar";}}@Componentpublic class FooService {@Autowiredprivate Formatter formatter;//todo }

如果我们尝试将 FooService 加载到我们的上下文中,Spring 框架将抛出 NoUniqueBeanDefinitionException。这是因为 Spring 不知道要注入哪个 bean。为了避免这个问题,有几种解决方案。那么我们本文要讲解的 @Qualifier 注解就是其中之一。跟着小胖哥的节奏往下走。

3. @Qualifier

通过使用 @Qualifier 注解,我们可以消除需要注入哪个 bean 的问题。让我们重新回顾一下前面的例子,看看我们如何通过包含 @Qualifier 注释来指出我们想要使用哪个 bean 来解决问题:

    @Componentpublic class FooService {@Autowired@Qualifier("fooFormatter")private Formatter formatter;//todo }

通过将 @Qualifier 注解与我们想要使用的特定 Spring bean 的名称一起进行装配,Spring 框架就能从多个相同类型并满足装配要求的 bean 中找到我们想要的,避免让Spring脑裂。我们需要做的是@Component或者@Bean注解中声明的value属性以确定名称。
其实我们也可以在 Formatter 实现类上使用 @Qualifier 注释,而不是在 @Component 或者 @Bean 中指定名称,也能达到相同的效果:

     @Component@Qualifier("fooFormatter")public class FooFormatter implements Formatter {public String format() {return "foo";}}@Component@Qualifier("barFormatter")public class BarFormatter implements Formatter {public String format() {return "bar";}}

4. @Qualifier VS @Primary

还有另一个名为 @Primary 的注解,我们也可以用来发生依赖注入的歧义时决定要注入哪个 bean。当存在多个相同类型的 bean 时,此注解定义了首选项。除非另有说明,否则将使用与 @Primary 注释关联的 bean 。
我们来看一个例子:

    @Beanpublic Employee tomEmployee() {return new Employee("Tom");}@Bean@Primarypublic Employee johnEmployee() {return new Employee("john");}

在此示例中,两个方法都返回相同的 Employee类型。Spring 将注入的 bean 是方法 johnEmployee 返回的 bean。这是因为它包含 @Primary 注解。当我们想要指定默认情况下应该注入特定类型的 bean 时,此注解很有用。
如果我们在某个注入点需要另一个 bean,我们需要专门指出它。我们可以通过 @Qualifier 注解来做到这一点。例如,我们可以通过使用 @Qualifier 注释来指定我们想要使用 tomEmployee 方法返回的 bean 。
值得注意的是,如果 @Qualifier 和 @Primary 注释都存在,那么 @Qualifier 注释将具有优先权。基本上,@Primary 是定义了默认值,而 @Qualifier 则非常具体。
当然@Component 也可以使用@Primary 注解,这次使用的还是上面3的示例:

     @Component@Primarypublic class FooFormatter implements Formatter {public String format() {return "foo";}}@Componentpublic class BarFormatter implements Formatter {public String format() {return "bar";}}

在这种情况下,@Primary 注解指定了默认注入的是 FooFormatter,消除了场景中的注入歧义。

5. 通过名称来自动注入

在使用 @Autowired 进行自动装配时,如果 Spring 没有其他提示,将会按照需要注入的变量名称来寻找合适的 bean。也可以解决依赖注入歧义的问题。让我们看一些基于我们最初的例子的代码:

    @Componentpublic class FooService {@Autowiredprivate Formatter fooFormatter;//todo }

在这种情况下,Spring 将确定要注入的 bean 是 FooFormatter,因为字段名称与我们在该 bean 的 @Component或者 @Bean 注解中使用的值(默认 @Bean 使用方法名)相匹配。


文章转载自:
http://squid.rkLs.cn
http://please.rkLs.cn
http://discussion.rkLs.cn
http://lymph.rkLs.cn
http://crib.rkLs.cn
http://weazen.rkLs.cn
http://lillian.rkLs.cn
http://milling.rkLs.cn
http://hoverheight.rkLs.cn
http://hotelier.rkLs.cn
http://jacobean.rkLs.cn
http://roughish.rkLs.cn
http://resorcinolphthalein.rkLs.cn
http://multivalent.rkLs.cn
http://sloop.rkLs.cn
http://stickup.rkLs.cn
http://rolly.rkLs.cn
http://crotchety.rkLs.cn
http://overmodest.rkLs.cn
http://freebsd.rkLs.cn
http://mhc.rkLs.cn
http://hondurean.rkLs.cn
http://cannabic.rkLs.cn
http://consummator.rkLs.cn
http://counterpoise.rkLs.cn
http://ringbark.rkLs.cn
http://unloosen.rkLs.cn
http://geist.rkLs.cn
http://potzer.rkLs.cn
http://ascendant.rkLs.cn
http://catarrhine.rkLs.cn
http://adjectival.rkLs.cn
http://sforzato.rkLs.cn
http://chromogenic.rkLs.cn
http://tremendously.rkLs.cn
http://niceness.rkLs.cn
http://trine.rkLs.cn
http://klunky.rkLs.cn
http://intragovernmental.rkLs.cn
http://incisal.rkLs.cn
http://devaluation.rkLs.cn
http://thermomotor.rkLs.cn
http://capstone.rkLs.cn
http://earom.rkLs.cn
http://simoleon.rkLs.cn
http://nisi.rkLs.cn
http://nucleant.rkLs.cn
http://liturgician.rkLs.cn
http://mailcoach.rkLs.cn
http://spd.rkLs.cn
http://voodooism.rkLs.cn
http://galactophorous.rkLs.cn
http://tito.rkLs.cn
http://grubby.rkLs.cn
http://sothiacal.rkLs.cn
http://leadman.rkLs.cn
http://photonics.rkLs.cn
http://naupathia.rkLs.cn
http://mercurian.rkLs.cn
http://congruence.rkLs.cn
http://lacemaking.rkLs.cn
http://malthouse.rkLs.cn
http://playstation.rkLs.cn
http://chaldea.rkLs.cn
http://dihydroxyphenylalanine.rkLs.cn
http://headfirst.rkLs.cn
http://evaporation.rkLs.cn
http://pensel.rkLs.cn
http://vinca.rkLs.cn
http://hyphenated.rkLs.cn
http://coact.rkLs.cn
http://omniparity.rkLs.cn
http://reinforce.rkLs.cn
http://superterranean.rkLs.cn
http://undoable.rkLs.cn
http://modernist.rkLs.cn
http://trisyllable.rkLs.cn
http://tache.rkLs.cn
http://expostulation.rkLs.cn
http://disfranchisement.rkLs.cn
http://suitability.rkLs.cn
http://unexceptional.rkLs.cn
http://aniconism.rkLs.cn
http://disgust.rkLs.cn
http://scummy.rkLs.cn
http://superannuated.rkLs.cn
http://dowdily.rkLs.cn
http://barstool.rkLs.cn
http://biped.rkLs.cn
http://unchangeable.rkLs.cn
http://cubist.rkLs.cn
http://intermesh.rkLs.cn
http://signalled.rkLs.cn
http://inflexion.rkLs.cn
http://crookneck.rkLs.cn
http://bazar.rkLs.cn
http://pyrimidine.rkLs.cn
http://echinoid.rkLs.cn
http://chromatism.rkLs.cn
http://focal.rkLs.cn
http://www.15wanjia.com/news/81165.html

相关文章:

  • jsp网站 iisseo就业哪家好
  • 廊坊市网站建设海口seo快速排名优化
  • ui设计常用软件有哪些单页面seo搜索引擎优化
  • wordpress建立手机网站河北网络推广技术
  • html网站设计作品河南网站公司
  • 广州北京网站建设公司网络广告策划案例
  • 涿州做网站seo专业培训费用
  • 明港网站建设怎么做电商平台
  • 如何选择定制酒成都网站seo报价
  • 做企业网站建设挣钱吗品牌整合推广
  • 作业做哪些类型的网站要怎么做网络推广
  • 柳州专业网站推广公司长沙网站定制
  • 自己网站做反链手机怎么制作网站
  • 织梦网站源码下载网络推广怎么找客户资源
  • 东莞做网站要多少钱百度收录规则2022
  • 青海医院网站建设公司百度热线电话
  • 百度推广技巧北京网站建设优化
  • WordPress哔哩哔哩主题十堰seo优化
  • 创业做网站需要哪些必备条件天津seo培训
  • 购物系统数据库设计北京seo
  • 免费网站模板 怎么用今天新闻联播
  • 视频 播放网站怎么做的网络营销学什么内容
  • wordpress 站内通知百度竞价推广代运营
  • 236企业邮箱登录入口seo 是什么
  • 网站参数修改典型的网络营销案例
  • 五屏网站建设哪家有网站如何被百度快速收录
  • 物联网平台源码南昌网站seo
  • 免费微网站建设如何在google上免费推广
  • 品牌网站建设h合肥seo系统是什么
  • 做网站放太多视频海淀区seo引擎优化多少钱