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
原创粉丝点击