问题描述
我首先整合的hibernate和spring,通过测试是成功的。然后开始struts2的整合,出现错误,请各位帮忙看下,是怎么个情况?涉及到的配置文件和错误如下: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的配置文件路径 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- 实例化spring容器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>===============================================================================================spring配置文件applicationContext.xml<?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:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:component-scan base-package="com.ssh"></context:component-scan><!-- 配置数据源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="org.gjt.mm.mysql.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> <property name="initialPoolSize" value="1"></property> <property name="minPoolSize" value="1"></property> <property name="maxPoolSize" value="300"></property> <property name="maxIdleTime" value="60"></property> <property name="acquireIncrement" value="5"></property> <property name="idleConnectionTestPeriod" value="60"></property></bean><!-- 配置sessionFactory --><bean id="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="mappingResources"><list><value>com/ssh/bean/Employee.hbm.xml</value></list></property><property name="hibernateProperties"><value>hibernate.dialect=org.hibernate.dialect.MySQL5Dialecthibernate.hbm2ddl.auto=updatehibernate.show_sql=truehibernate.format_sql=false</value></property></bean><bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sf"></property></bean><tx:annotation-driven transaction-manager="txManager"/><bean id="employeeservice" class="com.ssh.service.EmployeeService"></bean></beans>====================================================================================================struts.xml<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- <constant name="struts.objectFactory" value="spring"></constant>--><package name="employee" namespace="/employee" extends="struts-default"><action name="list" class="ermployeeAction"><result name="list">WEB-INF/page/employee.jsp</result></action></package></struts>===============================================================================================错误:Context initialization failed - <2011-03-11 12:23:39,765> ERROR (ContextLoader.java:215)org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeAction': Injection of resource fields failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.ssh.impl.EmployeeImpl] is defined: expected single matching bean but found 2: [employeeService, employeeservice]at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessAfterInstantiation(CommonAnnotationBeanPostProcessor.java:292)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)at java.security.AccessController.doPrivileged(Native Method)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3764)at org.apache.catalina.core.StandardContext.start(StandardContext.java:4216)at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)at org.apache.catalina.core.StandardService.start(StandardService.java:448)at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)at org.apache.catalina.startup.Catalina.start(Catalina.java:552)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.ssh.impl.EmployeeImpl] is defined: expected single matching bean but found 2: [employeeService, employeeservice]at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:621)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:431)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:409)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:537)at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:180)at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessAfterInstantiation(CommonAnnotationBeanPostProcessor.java:289)... 40 more这里只是进行一个框架整合的简单测试,涉及的文件不多,一个action 和关于业务逻辑的一个接口以及实现该接口的类。 问题补充:enet_java 写道
解决方案
引用org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeAction': Injection of resource fields failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.ssh.impl.EmployeeImpl] is defined: expected single matching bean but found 2: [employeeService, employeeservice]自动注入bean时发现有两个可用的bean, 不知注入哪一个,所以抛异常了。你的applicationContext.xml文件里有了这一行,容器就会在com.ssh目录下寻找bean了。引用<context:component-scan base-package="com.ssh"></context:component-scan> 下面这一行再次定义了一个EmployeeService的bean,导致重复。把这行注释掉后再试试看吧。引用<bean id="employeeservice" class="com.ssh.service.EmployeeService"></bean>
解决方案二:
在你的com.ssh.service.EmployeeService类前加@Service像这样:@Service@Transactionalpublic class EmployeeService {加完后把配置文件中的去掉:<bean id="employeeservice" class="com.ssh.service.EmployeeService"></bean>
解决方案三:
另外,你是不是action上写annotation了,然后spring配置文件也写了一个,也有这个可能。自己排除可能性
解决方案四:
引用No unique bean of type [com.ssh.impl.EmployeeImpl] is defined: expected single matching bean but found 2: 你是根据type来注入的,但是出现了相同type的bean,你改用根据id注入,就不会出现不唯一的bean异常了
解决方案五:
EmployeeImpl 因为你的service中使用 EmployeeImpl 对象了,所以需要在spring配置文件中定义一个Bean EmployeeImpl,注入到service层,引用<beanclass="com.the.bt.service.impl.DownloadFileServiceSpringImpl"><property name="baseDAO"><ref local="baseDAO" /></property></bean>类似上面的代码
解决方案六:
Struts配置文件中指定的ermployeeAction,在Spring里没配啊
解决方案七:
employeeAction 没有定义。如果通过spring来管理的话,在sring配置文件中增加<bean name="employeeAction " class="xxxx" singleton="false"></bean>的配置。
解决方案八:
EmployeeService 没有注入 employeeAction