在使用Spring框架中@Autowired标签时默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时,Spring 容器将抛出
BeanCreationException 异常,并指出必须至少拥有一个匹配的 Bean。
Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称,这样歧义就消除了,可以通过下面的方法解决异常。
@Qualifier("XXX") 中的 XX是 Bean 的名称,所以 @Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。
@Autowired 可以对成员变量、方法以及构造函数进行注释,而
@Qualifier 的标注对象是成员变量、方法入参、构造函数入参。
  1. @Autowired
  2. @Qualifier("configSearcherService")
  3. private SearchService service;
  4.  
  5. @Autowired
  6. @Qualifier("redisService")
  7. private RedisDelegateService redisService;
  1.  
configSearcherService和redisService已经在配置文件中配置。
applicationContext-services.xml文件部分如下:
  1. <pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:p="http://www.springframework.org/schema/p"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:jms="http://www.springframework.org/schema/jms"
  9. xmlns:amq="http://activemq.apache.org/schema/core"
  10. xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
  11. xsi:schemaLocation="http://www.springframework.org/schema/beans
  12. http://www.springframework.org/schema/beans/spring-beans.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop.xsd
  15. http://www.springframework.org/schema/context
  16. http://www.springframework.org/schema/context/spring-context.xsd
  17. http://www.springframework.org/schema/tx
  18. http://www.springframework.org/schema/tx/spring-tx.xsd">
  19.  
  20. <!-- 由于对同一个collection的查询对象,各个对象的查询条件是不可共享的,所以需要多实例运行,scope="prototype"是多实例,不写该属性,则为共享实例 -->
  21. <bean id="configSearcherService" class="com.solr.adjust.service.impl.SearchServiceImpl" scope="prototype"/>
  22. <bean id="redisService" class="com.solr.adjust.service.impl.RedisDelegateService">
  1.  

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

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

在web.xml文件中

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7.  
  8. <welcome-file-list>
  9. <welcome-file>index.html</welcome-file>
  10. <welcome-file>index.jsp</welcome-file>
  11. </welcome-file-list>
  12. <!-- 加载applicationContext-all.xml配置文件 -->
  13. <context-param>
  14. <param-name>contextConfigLocation</param-name>
  15. <param-value>
  16. classpath:applicationContext-all.xml
  17. </param-value>
  18. </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. Alpha冲刺一 (6/10)

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10004107.html 作业博客:https://edu.cnblogs.com/campus ...

  2. JS数组中级+高级技巧

    本文介绍JS数组一些比较进阶的方法: reverse:数组反转: join:(参数)以参数为连接符将数组拼接为字符串: 实例: var arr=[]; arr[3]="haha"; ...

  3. prefix sums--codility

    lesson 5: prefix sums 1. PassingCars 2. CountDiv 3. GenomicRangeQuery 4. MinAvgTwoSlice lesson 5: pr ...

  4. php在循环内外实例化类占用内存比较

    关于php类的实例化和内存的关系,可以这么说:只要有一个new 关键字就是创建一个对象,创建一个对象就是在内存中分配了一个空间. 代码1: 在循环外实例化类:class ABC{ public $nu ...

  5. docker基于commit命令创建支持ssh服务的镜像

    以centos为基础,目的使用ssh服务远程连接docker容器. 环境:宿主机centos7(宿主机ip地址为192.168.164.130),直接搜索docker的centos镜像,下载最新版本. ...

  6. [原创]JEECMS 自定义标签调用广告版位下的所有广告(利用广告管理管理首页幻灯片)

    JEECMS自带的只有[@cms_advertising]标签,并且官方没有给文档,用法: [@cms_advertising id='3']             <img src=&quo ...

  7. Centos 6 安装 配置 oracle11g R2

    1.安装centos6.3_64位: 下载地址:http://mirror.bit.edu.cn/centos/6.3/isos/x86_64/ CentOS-6.3-x86_64-bin-DVD1. ...

  8. Python 定期检查Build_setting的编译情况

    #!/usr/bin/python #_*_ coding:utf-8 _*_ import time from email.MIMEText import MIMEText from email.M ...

  9. jsp的session完成登陆功能

    login.jsp: <%@ page language="java" import="java.util.*" contentType="te ...

  10. c#中变量的作用域

    C#中的作用域和javascript中的作用域还是有区别的.呵呵 class Person { /* *确定C#变量作用域的2个规则. 1.类的字段所处的作用域等同于该字段所属类所在的作用域; * 2 ...