在使用Spring框架中@Autowired标签时默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时,Spring 容器将抛出
BeanCreationException 异常,并指出必须至少拥有一个匹配的 Bean。
Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称,这样歧义就消除了,可以通过下面的方法解决异常。
@Qualifier("XXX") 中的 XX是 Bean 的名称,所以 @Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。
@Autowired 可以对成员变量、方法以及构造函数进行注释,而
@Qualifier 的标注对象是成员变量、方法入参、构造函数入参。
@Autowired
@Qualifier("configSearcherService")
private SearchService service; @Autowired
@Qualifier("redisService")
private RedisDelegateService redisService;

configSearcherService和redisService已经在配置文件中配置。
applicationContext-services.xml文件部分如下:
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 由于对同一个collection的查询对象,各个对象的查询条件是不可共享的,所以需要多实例运行,scope="prototype"是多实例,不写该属性,则为共享实例 -->
<bean id="configSearcherService" class="com.solr.adjust.service.impl.SearchServiceImpl" scope="prototype"/>
<bean id="redisService" class="com.solr.adjust.service.impl.RedisDelegateService">

在applicationContext-all.xml文件中导入service配置文件

<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> <!-- getContext -->
<bean class="com.solr.adjust.entity.GlobalContext"></bean>
<import resource="applicationContext-services.xml"/>
<import resource="applicationContext-solrConfig.xml"/>
</beans>

在web.xml文件中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 加载applicationContext-all.xml配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext-all.xml
</param-value>
</context-param>

Spring中的注解配置-注入bean的更多相关文章

  1. Spring中基于注解方式管理bean

    操作步骤 第一步:导入相关jar包 spring IoC的基本包 Spring支持注解的Jar包 第二步:创建Spring配置文件,ApplicationContext.xml 引入约束和开启注解扫描 ...

  2. Spring中@Autowired 注解的注入规则

    默认根据类型,匹配不到则根据bean名字 1.声明一个service接口 public interface HelloService { void sayHello(); } 2.service接口的 ...

  3. Spring中三种配置Bean的方式

    Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...

  4. Spring MVC4 纯注解配置教程

    阅读本文需要又一定的sping基础,最起码要成功的运行过一个SpringMvc项目. 在传统的Spring项目中,我们要写一堆的XML文件.而这些XML文件格式要求又很严格,很不便于开发.而网上所谓的 ...

  5. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

  6. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

  7. Spring中Value注解的使用

    Spring中Value注解的使用 分类: Spring2014-08-16 17:28 2985人阅读 评论(0) 收藏 举报 有的时候我们定义了Properties文件,并且使用Spring的Pr ...

  8. spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式

    spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式[部分内容转载] 2018年03月27日 18:58:41 守望dfdfdf 阅读数:62更多 个人分类: 工 ...

  9. Spring之基于注解的注入

    对于DI使用注解,将不再需要在Spring配置文件中声明Bean实例.Spring中使用注解,需要在原有Spring运行环境基础上再做一些改变,完成以下三个步骤. (1)导入AOP的Jar包.因为注解 ...

随机推荐

  1. 知名第三方编译版tete009 Firefox 24.0

    Firefox除了官方版本上还有许多由爱好者自己编译修改的第三方版本. 其中 tete009 是十分流行的一个版本,目前tete009 Firefox 24.0 版本发布. tete009版Firef ...

  2. 作业要求20181023-4 Alpha阶段第2周/共2周 Scrum立会报告+燃尽图 03

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284] 版本控制:https://git.coding.net/liuyy08 ...

  3. tcpdump学习笔记

    简介     简单的说,tcpdump就是一个抓包工具,类似Wireshark.     tcpdump可以根据使用者的定义过滤/截取网络上的数据包,并进行分析.tcpdump可以将数据包的头部完全接 ...

  4. New Concept English Two 21 55

    $课文53  触电的蛇 544. At last firemen have put out a big forest fire in California. 消防队员们终于扑灭了加利福尼亚的一场森林大 ...

  5. SELU︱在keras、tensorflow中使用SELU激活函数

    arXiv 上公开的一篇 NIPS 投稿论文<Self-Normalizing Neural Networks>引起了圈内极大的关注,它提出了缩放指数型线性单元(SELU)而引进了自归一化 ...

  6. Jenkins配置slave遇到“无法启动该应用程序”的问题

    飞测说:最近在负责持续集成相关的工作,我们用的是jenkins+svn+maven+sonar, 今天在用slave这块出现了一个问题,排查了好久才解决,踩过的坑,现在和大家一起看看,希望对大家有帮助 ...

  7. Java中替换字符串中特殊字符+ 20150921

    需求:今天需要将字符串中的" +"转换程"%2B",但是"+"是正则表达式中的特殊字符,使用需要反斜杠转义,具体示范: String a=& ...

  8. 斯特灵(Stirling)数

    http://zh.wikipedia.org/wiki/%E6%96%AF%E7%89%B9%E7%81%B5%E6%95%B0 第一类:n个元素分成k个非空循环排列(环)的方法总数 递推式:s(n ...

  9. v4l2框架函数调用关系

    所有的设备节点和子设备节点都是通过__video_register_device()注册的 1.对于video设备节点: 用户空间ioctl(VIDIOC_S_FMT)---> v4l2_fop ...

  10. visual studio内置“iis”组件提取及二次开发

    简介 visual studio安装后会自带小型的“iis”服务器,本文就简单提取一下这个组件,自己做一个小型“iis”服务器吧.先来说用途吧(废话可绕过),比如在服务器上没有安装iis,或者给客户演 ...