Spring中的注解配置-注入bean
来源:互联网 发布:货物进出库软件 编辑:程序博客网 时间:2024/05/16 16:15
在使用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;
</pre><div class="para" style="color:rgb(51,51,51); margin:15px 0px 5px; text-indent:2em; line-height:24px; font-family:arial,宋体,sans-serif; font-size:14px">configSearcherService和redisService已经在配置文件中配置。</div><div class="para" style="color:rgb(51,51,51); margin:15px 0px 5px; text-indent:2em; line-height:24px; font-family:arial,宋体,sans-serif; font-size:14px">applicationContext-services.xml文件部分如下:</div><pre code_snippet_id="515564" snippet_file_name="blog_20141111_3_96376" name="code" class="java"><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>
<!-- mvc扫描注解,自动搜索@action标注的类,使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 --><context:component-scan base-package="com.solr.adjust.controller" > <context:include-filter type="regex" expression=".*"/></context:component-scan > <mvc:annotation-driven />
在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>
0 0
- Spring中的注解配置-注入bean
- Spring配置使用注解注入bean
- 【Spring】注解注入bean
- Spring注解注入bean
- Spring注解自动注入Bean
- Spring bean 注解方式注入
- spring 使用注解注入bean
- spring注解自动注入bean
- Spring注解自动注入Bean
- Spring Bean装配--@Bean注解注入说明
- Spring -- 注解配置Bean
- spring 注解配置bean
- Spring - 注解配置Bean
- Spring Bean配置:注解配置
- Spring Bean自动装配和注解注入
- Spring基础 注解实现Bean依赖注入
- spring使用注解注入bean理解
- Spring Bean装配--Autowired注解注入说明
- 第十一周项目六:1000内的回文数
- 如何使用android-support-v7-appcompat
- 手机端页面收获
- Oracle的txt数据导入【Oracle数据库的Load详解】
- Problem 010——UVa 455 - Periodic Strings
- Spring中的注解配置-注入bean
- 使用yum安装CDH Hadoop集群
- 四个开源商业智能平台比较(一)
- Android开发之ImageView ScaleType属性
- ZXing拍码后区分扫描到的是一维码、二维码、其他码
- 第11周项目6-可逆素数
- 强制让SQL走谓词推入
- ZOJ3471 Most Powerful
- No PostgreSQL clusters exist; see "man pg_createcluster" 解决方案之一