struts2.3.4.1+spring3.1.1+hibernate3.6.10框架整合搭建,解决UnsatisfiedDependencyException



环境: struts2.3.4.1+spring3.1.1+hibernate3.6.10+MySQL 5.5

前期配置没什么大问题,除了几个jar包引入的时候出了点状况,现在把所需的jar包截图,大多数在下载的SSH框架中都有,没有的单独下载吧



整合完毕,配置SSH,主要配置文件如下:

Web.xml:



	
	
		index.jsp
	
	
	
		struts2
		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
	
	
		struts2
		*.action
	
	
	
		contextConfigLocation
		classpath:applicationContext.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
		org.springframework.web.util.Log4jConfigListener
	
	
		log4jConfigLocation
		classpath:log.properties
	

Struts:




	
		
			/pages/userInfoList.jsp
		
	
        

Spring:



	
	
		
		
		
		
		

		
		
		
		
		

		
		

		
		

		
		

		

	


	
		
			classpath:database.properties
		
	
	
		
		
		
			
				
					classpath:/com/mytest/dto/*.hbm.xml
				
			
		
		
			
				20

				
					org.hibernate.dialect.MySQL5Dialect
				
				false
			
		
	
	
	
	
		
	

	
		
			
			
			
		
	

	

	
		
	

	
	
		
	

	
		
	

	
		 
	
		


Hibernate:




	
		
			
		
		
			
		
		
			
		
	



然后启动,报异常如下:


严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Unsatisfied dependency expressed through bean property 'eventListeners': : Error creating bean with name 'transactionManager' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sessionFactory': FactoryBean which is currently in creation returned null from getObject; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sessionFactory': FactoryBean which is currently in creation returned null from getObject
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1199)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1091)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)……

……


百度、Google了一堆,查了一堆API,去官网看了框架版本change logs,demo……之后终于找到原因了

原来是Spring配置的beans的bean装配属性default-autowire="autodetect",配置成了自动,所以产生了这个异常。于是删除它,改为在每个Dao层手动注入.

重启,运行,一切正常


问题暂时解决了,不过还是有疑问,因为这样在每个DAO层的bean中都要手动通过property属性,来配置注入,不知道default-autowire="autodetect" 这个属性要怎么配置才能实现自动注入?


你可能感兴趣的:(Hibernate,Spring,Struts)