ssh整合报错严重: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxx'
错误描述:eclipse整合ssh的时候 报不能创建名字为xxx的对象
1 信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7ae0c7c3: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,orderAction,dataSource,sessionFactory,txManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy 2 2018-7-4 22:14:45 org.springframework.web.context.ContextLoader initWebApplicationContext 3 严重: Context initialization failed 4 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderAction': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.lljf.vo.OrderVO com.lljf.action.OrderAction.orderVO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.lljf.vo.OrderVO] 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)} 5 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) 6 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146) 7 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) 8 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) 9 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296) 10 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 11 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293) 12 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 13 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628) 14 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) 15 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) 16 at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) 17 at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) 18 at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) 19 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4210) 20 at org.apache.catalina.core.StandardContext.start(StandardContext.java:4709) 21 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) 22 at org.apache.catalina.core.StandardHost.start(StandardHost.java:822) 23 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) 24 at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) 25 at org.apache.catalina.core.StandardService.start(StandardService.java:525) 26 at org.apache.catalina.core.StandardServer.start(StandardServer.java:754) 27 at org.apache.catalina.startup.Catalina.start(Catalina.java:595) 28 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 29 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 30 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 31 at java.lang.reflect.Method.invoke(Method.java:597) 32 at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 33 at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) 34 Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.lljf.vo.OrderVO com.lljf.action.OrderAction.orderVO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.lljf.vo.OrderVO] 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)} 35 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517) 36 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 37 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286) 38 ... 28 more 39 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.lljf.vo.OrderVO] 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)} 40 at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988) 41 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858) 42 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770) 43 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489) 44 ... 30 more 45 2018-7-4 22:14:45 org.apache.catalina.core.StandardContext listenerStart 46 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener 47 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderAction': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.lljf.vo.OrderVO com.lljf.action.OrderAction.orderVO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.lljf.vo.OrderVO] 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)} 48 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) 49 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146) 50 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) 51 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) 52 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296) 53 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 54 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293) 55 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 56 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628) 57 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) 58 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) 59 at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) 60 at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) 61 at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) 62 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4210) 63 at org.apache.catalina.core.StandardContext.start(StandardContext.java:4709) 64 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) 65 at org.apache.catalina.core.StandardHost.start(StandardHost.java:822) 66 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) 67 at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) 68 at org.apache.catalina.core.StandardService.start(StandardService.java:525) 69 at org.apache.catalina.core.StandardServer.start(StandardServer.java:754) 70 at org.apache.catalina.startup.Catalina.start(Catalina.java:595) 71 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 72 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 73 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 74 at java.lang.reflect.Method.invoke(Method.java:597) 75 at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 76 at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) 77 Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.lljf.vo.OrderVO com.lljf.action.OrderAction.orderVO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.lljf.vo.OrderVO] 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)} 78 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517) 79 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 80 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286) 81 ... 28 more 82 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.lljf.vo.OrderVO] 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)} 83 at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988) 84 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858) 85 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770) 86 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489) 87 ... 30 more 88 2018-7-4 22:14:45 org.apache.catalina.core.StandardContext start 89 严重: Error listenerStart 90 2018-7-4 22:14:45 org.apache.catalina.core.StandardContext start 91 严重: Context [/lljf] startup failed due to previous errors 92 2018-7-4 22:14:45 org.apache.catalina.core.ApplicationContext log 93 信息: Closing Spring root WebApplicationContext 94 2018-7-4 22:14:45 com.mchange.v2.log.MLog <clinit> 95 信息: MLog clients using java 1.4+ standard logging. 96 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". 97 SLF4J: Defaulting to no-operation (NOP) logger implementation 98 SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 99 2018-7-4 22:14:45 org.apache.coyote.http11.Http11Protocol start 100 信息: Starting Coyote HTTP/1.1 on http-8080 101 2018-7-4 22:14:45 org.apache.jk.common.ChannelSocket init 102 信息: JK: ajp13 listening on /0.0.0.0:8009 103 2018-7-4 22:14:45 org.apache.jk.server.JkMain start 104 信息: Jk running ID=0 time=0/16 config=null 105 2018-7-4 22:14:45 org.apache.catalina.startup.Catalina start 106 信息: Server startup in 2070 ms
在网上找了好多发现都与自己的问题不相符,我把代码删除到只剩下一个类,发现是使用spring的注解管理对象的时候只要加上@Controller,@Repository这样的注解就会报上述的错误。分析其原因是struts2交由spring管理的时候需要添加@Scope("prototype")这个注解。
也就是将struts2中的action交给spring管理有2中方式:
将struts2中的action交给spring管理
1.xml方式
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: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="testAction" class="com.je.app.web.JeTestAction" scope="prototype"></bean> </beans>
sturts.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> <package name="je" extends="struts-default" namespace="/je"> <!-- Test--> <action name="test" class="testAction"> <result name="invalid.token">/pages/je/test.jsp</result> </action> </package> </struts>
2.注解方式
sturts.xml
注意:如果没有指定scope就会出现action属性保存(验证)后一直存在的问题,导致后面的保存(验证)根本没有进行
所以在spring中配置action时要指定scope属性为prototype
配置文件中加入scope="prototype"
注解的话要加上@Scope("prototype")
这样就ok了
默认的spring的bean的周期是单态的(Singleton)
对于每一次请求就不会生成新的实例