相关疑难解决方法(0)

什么是NoSuchBeanDefinitionException以及如何解决它?

NoSuchBeanDefinitionException在Spring中解释以下关于异常的内容:

  • 这是什么意思?
  • 它会在什么条件下抛出?
  • 我该怎样预防呢?

本文旨在对NoSuchBeanDefinitionException使用Spring的应用程序中出现的问题进行全面的问答.

java spring applicationcontext

51
推荐指数
1
解决办法
6万
查看次数

使用独特bean进行Spring自动装配:Spring期望单个匹配bean但找到2

我正在尝试使用Spring为webapp自动装配一些bean(用于依赖注入).一个控制器bean包含另一个bean,而另一个bean又拥有另一组bean的hashmap.目前,地图只有一个条目.当我在tomcat中运行并调用该服务时,我得到一个错误,说第二个bean(保存在控制器中)不是唯一的

No unique bean of type [com.hp.it.km.search.web.suggestion.SuggestionService] is defined: expected single matching bean but found 2: [suggestionService, SuggestionService]
Run Code Online (Sandbox Code Playgroud)

我无法看到我在两次定义bean的位置,但是我是Spring的新手并且自动装配,所以我可能会遗漏一些基本的东西.下面列出的xml和2类的源代码......

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:context="http://www.springframework.org/schema/context"     xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="com.hp.it.km.search.web.suggestion" />
<mvc:annotation-driven />
<context:annotation-config />

<bean id="SuggestionController" class="com.hp.it.km.search.web.suggestion.SuggestionController">
    <property name="service">
        <ref bean="SuggestionService" />
    </property>
</bean>

<bean id="SuggestionService" class="com.hp.it.km.search.web.suggestion.SuggestionService">
    <property name="indexSearchers"> 
         <map>
            <entry key="KMSearcher"> <ref bean="KMSearcherBean"></ref></entry>
        </map>
    </property>
</bean>

<bean id="KMSearcherBean" class="com.hp.it.km.search.web.suggestion.SuggestionIndexSearcher">
      <constructor-arg index="0" value="KMSearcher" />
      <constructor-arg index="1" value="C://dev//workspace//search-restful-webapp//src//main//resources//indexes//keyword" />
</bean>
Run Code Online (Sandbox Code Playgroud)

带有自动控制器和服务bean的类asscoaites在这里......

@Controller
public class …
Run Code Online (Sandbox Code Playgroud)

spring annotations dependency-injection spring-mvc autowired

43
推荐指数
4
解决办法
12万
查看次数