遇到一个不唯一的异常:
严重: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demoController': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.swunDms.swunDms.service.UserManager org.swunDms.swunDms.web.DemoController.userManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.swunDms.swunDms.service.UserManager] is defined: Unsatisfied dependency of type [interface org.swunDms.swunDms.service.UserManager]: expected at least 1 matching beanCaused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.swunDms.swunDms.service.UserManager org.swunDms.swunDms.web.DemoController.userManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.swunDms.swunDms.service.UserManager] is defined: Unsatisfied dependency of type [interface org.swunDms.swunDms.service.UserManager]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)我的控制器:
@Controller
public class DemoController {
    
@Autowired
private UserManager userManager;

@RequestMapping("/view.do")
public String viewHandler(String url) {
return url;
}

@RequestMapping("/viewPage.do")
public String viewParameter(HttpServletRequest request,ModelMap model,String url){
Enumeration parameterNames=request.getParameterNames();
while(parameterNames.hasMoreElements()){
String parameterName=parameterNames.nextElement().toString();
model.addAttribute(parameterName, request.getParameter(parameterName));
}
return url;
}

@RequestMapping("/demo.do")
public String demo(ModelMap model){
User user=userManager.get(1L);
model.addAttribute("user",user);
return "demo";
}
}
我的UserManager的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="userManager" class="org.swunDms.swunDms.service.impl.UserManagerImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>
</beans>
我的DAO层的配置文件:
<?xml version="1.0" encoding="UTF-8"?><!--
  - Application context definition for JPetStore's business layer.
  - Contains bean references to the transaction manager and to the DAOs in
  - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
  -->
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  
    <bean id="userDao"  class="org.swunDms.swunDms.dao.impl.UserDaoImpl">
       <constructor-arg><value>org.swunDms.swunDms.domain.User</value></constructor-arg>
       <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
</beans>
我的WEB.XML:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔
此参数用于后面的Spring Context Loader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param> <!-- Character Encoding filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter> <!-- Hibernate Open Session In View filter-->
<filter>
<filter-name>osivFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter> <filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <filter-mapping>
<filter-name>osivFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!--Spring ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>        <!--  Spring MVC 的Servlet,它将加载WEB-INF/swunDms-servlet.xml 的
    配置文件,以启动Spring MVC模块-->
    <servlet>
        <servlet-name>swunDms</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>    <servlet-mapping>
        <servlet-name>swunDms</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> <!-- session超时定义,单位为分钟 -->
<session-config>
<session-timeout>20</session-timeout>
</session-config> <error-page>
<error-code>404</error-code>
<location>/common/404.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/common/403.jsp</location>
</error-page>
</web-app>我的<?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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
     
    <!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
    <context:component-scan base-package="org.swunDms.swunDms.web"/>    <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>    <!--  ③:对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>
大哥们帮我看看问题。求救。我的UserManager没有定义几个。就一个。

解决方案 »

  1.   

    是说没找到,不是多了是不是主spring配置文件没有import另外的那个配置文件
      

  2.   

    配置文件配好了的,我是再WEB.XML中配置的APPLICATION等配置文件的
      

  3.   

    ApplicationContext.xml
    加入<import xxx="UserManager.xml">试试
      

  4.   

    你好,我也遇到相同的问题,是在做spring2.5+junit4.4的时候遇到的,不知道你现在解决没?如何解决的?