struts2-2.2.1+hibernate3.3.1+spring2.5.6整合

问题描述

我首先整合的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&amp;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

时间: 2024-12-19 07:44:33

struts2-2.2.1+hibernate3.3.1+spring2.5.6整合的相关文章

在SSH(struts2.21+hibernate3.3.2+spring2.5)整合中遇到一个问题:

问题描述 在SSH(struts2.21+hibernate3.3.2+spring2.5)整合中遇到一个问题:菜鸟求解~~知道的大师们给个解决方案把~~~目的:在容器启动后,自动从数据库中读出数据然后在WEB的application范围内写入数据 ,这样在index.jsp,里只要直接读取application数据就行了,不每次都过action.(都是一些一般不变的数据.且数据不多)试验:1.在把Action设为单例后,容器启动后会自动运行,不能访问spring里的bean,及在构造方法里打印

MyEclipse搭建SSH框架 Struts2.1+Spring3.0+hibernate3.3

原文http://www.cnblogs.com/AXDY-QIN/archive/2013/01/21/SSH.html 用MyEclipse搭建SSH框架 Struts2.1+Spring3.0+hibernate3.3 1.         配置Tomcat 配置Tomcat所在路径 配置Tomcat使用JDK版本 如果Tomcat为7.0则添加Tomcat-juli.jar包 2.         new一个web project. 2.右键项目,为项目添加Struts支持.   点击F

struts2+spring2.5+hibernate整合的问题

问题描述 ssh整合的时候不报错,但插入的时候数据库也没有数据,控制台把insert语句已经打出来了,这是什么情况,请大侠们帮忙看看 applicationContext.xml 配置如下: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http

struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成简单demo_java

近期公司要开发新的项目,要用struts2+mybatis+spring框架,所以学习了下,来自己的博客发表下,希望能给大家带来帮助! 主要实现用户的增删改查操作 1.导入相应的jar包 2.配置web.xml主要是配置struts2和spring web.xml文件内容如下: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="ht

做spring2.5+jpa整合时,出现org.springframework.beans.NotWritablePropertyException,请高手指教

问题描述 org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'entityManagerFactory'definedinclasspathresource[applicationContext.xml]:Errorsettingpropertyvalues;nestedexceptionisorg.springframework.beans.NotWritablePropertyEx

SSH2完美整合流程

教程流程是重点,应熟练掌握.本教程做简单的账号注册演示,所用工具:Myeclipse-6.5.Struts-2.2.3. Hibernate-3.2.Spring-2.0.MySql,本教程出自北大青鸟成都锦江校区(原文链接 http://www.scbdqn.com/course/netjava/3172.html),转载请注明原处,谢谢. 一.利用工具快速搭建环境 1.新建一个名为ssh2的Web项目 2.把STRUTS2的依赖JAR包添加到项目工程中.用WinRar解压Struts-2.2

重新学习之spring第四个程序,整合struts2+hibernate+spring

第一步:导入三大框架的jar包(struts2.3.16.1+hibernate3.2+spring3.2.4)   第二步:编写web.xml 和struts.xml和applicationContext.xml和applicationContext-service.xml和application-actionContext.xml和applicationContext-dao.xml web.xml 1 <?xml version="1.0" encoding="U

大家给我的简历一点意见

问题描述 本人刚离职因为要去新疆出差几个月,家里极力反对所以准备换公司,简历刚完成请大家给点意见!多谢!!求职简历个人简介:姓名:***性别:男年龄:23岁民族:汉族籍贯:河出生年月:1987.12.18现住址:西北旺学历:本科毕业院校:河北工程大学移动电话:13146****E-mail:*****@163.com求职目标:职位:J2EE软件工程师项目经验:新疆人力资源管理系统:时间:08年03月--08年12月开发工具:普元EOS,EXT,JDK5.0项目背景:新疆自治区地域辽阔,人员复杂

谈谈对Struts多模块的认识

首先,建一个web project,然后添加对hibernate和spring的支持,我们使用的是hibernate3.1和spring2.0,然后导入dwr.jar和commons-pool-1.3.jar(不知道myeclipse怎么搞的,添加Spring功能支持的时候就有了commons-dbcp.jar,居然没有其依赖的commons-pool-x.jar,只好单独添加了,另外,需要将Spring2.0 AOP Liberaries里的asm2.2.3.jar删除,因为好像和Hibera