spring spring mvc hibernate整合遇到的异常

时间:2021-10-16 23:16:59
ssh-test12-06版本,异常解决过程
[org.springframework.web.context.ContextLoader]Context initialization failed
把web.xml中的hibernate.cfg.xml删掉解决了

  [org.springframework.web.context.support.XmlWebApplicationContext]Exception encountered during context initialization - cancelling refresh attempt
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is java.lang.IncompatibleClassChangeError: class net.sf.cglib.core.DebuggingClassWriter has interface org.objectweb.asm.ClassVisitor as super class

检查了jar包,发现asm包有两个,修改pom.xml文件,删掉了一个asm包并没有解决问题。
将cglib的版本改为2.2解决了这个问题 该版本中的DebuggingClassWriter的父类为ClassWriter

又出现了
[org.hibernate.impl.SessionFactoryImpl]Unable to construct current session context [org.springframework.orm.hibernate4.SpringSessionContext]
  java.lang.NoSuchMethodException : org.springframework.orm.hibernate4.SpringSessionContext.<init>(org.hibernate.engine.SessionFactoryImplementor)
解决不了,改数据源配置

github上找了一个ssh的demo,使用的是Druid,其版本配置为spring4.2+hibernate5
https://github.com/liyifeng1994/ssm  ssm demo,全过程


aop配置问题
配置事务时,一定注意expression="execution(* com.crm..*(..))" 应该为 expression="execution(* com.crm.*.*(..))" ,这样,切点才定位到方法上了。


java.lang.UnsupportedOperationException : Not supported by BasicDataSource 
异常处理:
< bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" >改为
< bean id = "dataSource" class = " org.springframework.jdbc.datasource.DriverManagerDataSource " >


问题背景:
工程为maven工程,ContextLoaderListener类位于spring-web-3.1.0.RELEASE.jar包中。检查了maven的pom.xml,依赖引入正常。在工程Maven Dependencies视图中也能看到spring-web-3.1.0.RELEASE.jar包被正常引入进来了。
 
错误原因:
进入到tomcat的部署路径.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\下检查了一下,发现工程部署后在WEB-INF文件夹下没有生成lib目录,正常情况下,会生成lib目录并把工程的所有依赖jar包都部署到该目录下。
 
解决方案:
1.右键点击项目--选择Properties
选择Deployment Assembly,在右边点击Add按钮,在弹出的窗口中选择Java Build Path Entries
2.点击Next,选择Maven Dependencies
3.点击Finish,然后可以看到已经把Maven Dependencies添加到Web应用结构中了。
操作完后,重新部署工程,不再报错了。然后我们再到 .metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ 目录下,发现工程WEB-INF目录下自动生成了lib目录,并且所有的依赖jar包也都已经部署进来。问题因此解决。


找不到jdbc.properties中的变量的错误:{jdbc_driverClassName}不起作用,的解决方案
在spring-hibernate.xml文件中加入 < context:property-placeholder location = "classpath*:config.properties" />

找不到spring.xml的处理
classpath:spring.xml -> classpath*:spring.xml


页面跳转问题的处理
暂未处理完毕


A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/demo]]

解决方法:
解决这个问题的关键在于错误信息中的:
[plain]   view plain   copy
  1. Servlet mapping specifies an unknown servlet name Action  
打开项目下的“src\main\webapp\WEB-INF\web.xml”文件,查找类似下面的节点:
[html]   view plain   copy
  1. <servlet-mapping>  
  2.     <servlet-name>Action</servlet-name>  
  3.     <url-pattern>/public/Action</url-pattern>  
  4. </servlet-mapping>  
将这个节点删除,问题就解决了。


spring默认加载的配置文件名demo-servlet.xml
<servlet>
    <servlet-name>demo</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>demo</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


org.springframework.beans.factory.BeanCreationException : Error creating bean with name 'userinfoController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException : Could not autowire field: private com.demo.service.IUserinfoService com.demo.controller.UserinfoController.userinfoServiceImpl; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException : No qualifying bean of type [com.demo.service.IUserinfoService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=userinfoServiceImpl)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues( AutowiredAnnotationBeanPostProcessor.java:334 )
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean( AbstractAutowireCapableBeanFactory.java:1214 )
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean( AbstractAutowireCapableBeanFactory.java:543 )
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean( AbstractAutowireCapableBeanFactory.java:482 )
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject( AbstractBeanFactory.java:306 )
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton( DefaultSingletonBeanRegistry.java:230 )
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean( AbstractBeanFactory.java:302 )
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean( AbstractBeanFactory.java:197 )
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons( DefaultListableBeanFactory.java:772 )
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization( AbstractApplicationContext.java:839 )
      at org.springframework.context.support.AbstractApplicationContext.refresh( AbstractApplicationContext.java:538 )
      at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext( FrameworkServlet.java:668 )
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext( FrameworkServlet.java:634 )
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext( FrameworkServlet.java:682 )
      at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext( FrameworkServlet.java:553 )
      at org.springframework.web.servlet.FrameworkServlet.initServletBean( FrameworkServlet.java:494 )
      at org.springframework.web.servlet.HttpServletBean.init( HttpServletBean.java:136 )
      at javax.servlet.GenericServlet.init( GenericServlet.java:158 )
      at org.apache.catalina.core.StandardWrapper.initServlet( StandardWrapper.java:1227 )
      at org.apache.catalina.core.StandardWrapper.loadServlet( StandardWrapper.java:1140 )
      at org.apache.catalina.core.StandardWrapper.load( StandardWrapper.java:1027 )
      at org.apache.catalina.core.StandardContext.loadOnStartup( StandardContext.java:5038 )
      at org.apache.catalina.core.StandardContext.startInternal( StandardContext.java:5348 )
      at org.apache.catalina.util.LifecycleBase.start( LifecycleBase.java:145 )
      at org.apache.catalina.core.ContainerBase$StartChild.call( ContainerBase.java:1408 )
      at org.apache.catalina.core.ContainerBase$StartChild.call( ContainerBase.java:1398 )
      at java.util.concurrent.FutureTask.run( FutureTask.java:262 )
      at java.util.concurrent.ThreadPoolExecutor.runWorker( ThreadPoolExecutor.java:1145 )
      at java.util.concurrent.ThreadPoolExecutor$Worker.run( ThreadPoolExecutor.java:615 )
      at java.lang.Thread.run( Thread.java:745 )
Caused by: org.springframework.beans.factory.BeanCreationException : Could not autowire field: private com.demo.service.IUserinfoService com.demo.controller.UserinfoController.userinfoServiceImpl; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException : No qualifying bean of type [com.demo.service.IUserinfoService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=userinfoServiceImpl)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject( AutowiredAnnotationBeanPostProcessor.java:573 )
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject( InjectionMetadata.java:88 )
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues( AutowiredAnnotationBeanPostProcessor.java:331 )
      ... 29 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException : No qualifying bean of type [com.demo.service.IUserinfoService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=userinfoServiceImpl)}
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException( DefaultListableBeanFactory.java:1373 )
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency( DefaultListableBeanFactory.java:1119 )
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency( DefaultListableBeanFactory.java:1014 )
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject( AutowiredAnnotationBeanPostProcessor.java:545 )
      ... 31 more

这个异常主要是因为注入bean的顺序导致,在spring-servlet配置文件中,应该按dao->service->controller的顺序扫描各个包。
正确的spring-servlet扫描包的过程:
      <!-- 注解扫描包  -->
      <context:component-scan base-package="com.demo.dao.impl" />
      <context:component-scan base-package="com.demo.services.impl" />
      <context:component-scan base-package="com.demo.controllers" />
 
原因:controller里面的方法依赖了service,而service又依赖了dao
      
错误的配置
    < context:component-scan base-package = "com.demo.controllers" />
    <!-- 自动扫描 dao 和service包(自动注入) -->
     < context:component-scan base-package = "com.demo.services.impl" />
     < context:component-scan base-package = "com.demo.dao.impl" />