【问题标题】:Suddenly my spring.xml won't instantiate my classes and Error Exception occurs突然我的 spring.xml 不会实例化我的类并且发生错误异常
【发布时间】:2012-11-13 12:24:59
【问题描述】:

我遇到了一个我无法理解的奇怪错误...当我运行我的 Web 项目时...我的 jsp 显示出来了。但是点击提交底部后,它挂起然后错误结果。我无法理解我做错了什么......这就像 3 小时试图检查我的问题。但现在我需要一些帮助.. 谢谢大家。

Unable to instantiate Action, lotmovement.action.RegisterAction,  defined for 'register' in namespace '/'Error creating bean with name 'registeraction' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [lotmovement.action.RegisterAction]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registeraction' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [lotmovement.action.RegisterAction]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registeraction' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [lotmovement.action.RegisterAction]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registeraction' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [lotmovement.action.RegisterAction]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registeraction' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: [spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [lotmovement.action.RegisterAction]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registeraction' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [lotmovement.action.RegisterAction]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registeraction' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [lotmovement.action.RegisterAction]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registeraction' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [lotmovement.action.RegisterAction]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [spring.xml]; nested exception is java.lang.StackOverflowError
    org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:115)
    org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:61)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:877)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:839)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    java.security.AccessController.doPrivileged(Native Method)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    lotmovement.action.RegisterAction.<init>(RegisterAction.java:27)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    java.lang.Class.newInstance0(Class.java:372)
    java.lang.Class.newInstance(Class.java:325)
    com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:131)
    com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:162)
    com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:151)
    com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:121)
    com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:300)
    com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:400)
    com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
    org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
    org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
    com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:501)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:432)

而在 tomcat 服务器上,这不断重复..

Nov 26, 2012 3:10:51 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2d43b35f: defining beans [userprofile,registeraction,insertuserprofile,entitystart,recordexistuserprofile]; root of factory hierarchy
Nov 26, 2012 3:10:51 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@50016db: defining beans [userprofile,registeraction,insertuserprofile,entitystart,recordexistuserprofile]; root of factory hierarchy
Nov 26, 2012 3:10:51 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7ef1277f: defining beans [userprofile,registeraction,insertuserprofile,entitystart,recordexistuserprofile]; root of factory hierarchy
Nov 26, 2012 3:10:51 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3d510eb6: defining beans [userprofile,registeraction,insertuserprofile,entitystart,recordexistuserprofile]; root of factory hierarchy
Nov 26, 2012 3:10:51 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@66893099: defining beans [userprofile,registeraction,insertuserprofile,entitystart,recordexistuserprofile]; root of factory hierarchy
Nov 26, 2012 3:17:59 PM org.apache.catalina.startup.HostConfig checkResources
INFO: Undeploying context [/LotMovement_5spingtest]

这是我的 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"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="userprofile"  class="lotmovement.business.entity.UserProfile">

    </bean>

    <bean id="registeraction"  class="lotmovement.action.RegisterAction">

    </bean>  

    <bean id="insertuserprofile"  
          class="lotmovement.business.crud.InsertUserProfile">

    </bean>


    <bean id="entitystart"  
          class="lotmovement.business.crud.EntityStart">

    </bean>

    <bean id="recordexistuserprofile"  
           class="lotmovement.business.crud.RecordExistUserProfile">

    </bean>


</beans>

这是我的一些代码..

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lotmovement.action;

import com.opensymphony.xwork2.ActionSupport;
import lotmovement.business.crud.EntityStart;
import lotmovement.business.crud.InsertUserProfile;
import lotmovement.business.crud.RecordExistUserProfile;
import lotmovement.business.entity.UserProfile;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author god-gavedmework
 */
public class RegisterAction extends ActionSupport {

    private static String userId;
    private static String password;
    private static String firstName;
    private static String lastName;
    private static int securityLevel;
    ApplicationContext context =
            new ClassPathXmlApplicationContext("spring.xml");
    EntityStart es = (EntityStart) context.getBean("entitystart");

    @Override
    public void validate() {
        es.StartDbaseConnection();
        RecordExistUserProfile reup =
                (RecordExistUserProfile) context.getBean("recordexistuserprofile");
        if (reup.checkrecordexist()) {
            addActionError("record already exist.");
        }
    }

    @Override
    public String execute() {



        InsertUserProfile iup =
                (InsertUserProfile) context.getBean("insertuserprofile");



        iup.Insert();


        return SUCCESS;
    }

这是我的 WEB.XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>register.jsp</welcome-file>
    </welcome-file-list>
</web-app>

【问题讨论】:

    标签: java spring struts2 netbeans-7


    【解决方案1】:

    您正在使用 RegisterAction 中的这一行创建一个无限循环

    ApplicationContext context =
            new ClassPathXmlApplicationContext("spring.xml");
    

    循环是你调用 Spring Container,它创建一个 RegisterAction,它(如上所示)创建一个 Spring Container,它创建一个 RegisterAction....

    您不应该直接在 Spring Bean 中引用 ApplicationContext。您应该使用配置(setter 或构造函数注入)来设置 bean 的依赖项。你需要这样做

    spring.xml

    <bean id="insertUserProfile" class="lotmovement.business.crud.InsertUserProfile"/>
    
    <bean id="registeraction"  class="lotmovement.action.RegisterAction">
      <property name="insertUserProfile" ref="insertUserProfile"/>
    </bean>  
    

    然后您将添加一个方法 setInsertUserProfile 到您的 RegisterAction。 Spring 将从您的上下文中使用指定的 bean 调用 setter(在本例中,是一个新的 insertUserProfile)。您需要对从上下文中引用的所有 bean 执行此操作。

    您可能想对 Spring 构造函数注入进行一些研究,因为它是注入永远不需要更改的依赖项的“更好”方式。

    【讨论】:

    • 我所做的是在每一堂课上,我有这个。 ApplicationContext 上下文 = 新 ClassPathXmlApplicationContext("spring.xml");。所以你的意思是我不会再用这个了?
    • 我会研究你的建议。
    • 正确,您的 spring.xml 中的任何类中都不应该有 ApplicationContext。
    • 谢谢你,我会检查你的建议。
    • 我认为 ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");我认为仅用于带有 main() 的类。但它在 Web 应用程序中没有用。
    【解决方案2】:

    杰夫的回答很到位。它很好地解释了什么是错的,什么是你不应该做的。

    您缺少的部分是在 web.xml 中注册应用程序上下文:

    Loading context in Spring using web.xml

    这是一个更正,加上 jeff 的建议,应该会让你上路。

    【讨论】:

    • 谢谢达菲莫。我会检查这个
    【解决方案3】:

    为什么在使用 Struts 2 时要自己处理 Spring 容器?您需要 Struts 2 Spring 插件。

    http://struts.apache.org/2.3.4.1/docs/spring-plugin.html

    虽然其他答案很好并且完美地描述了此代码的问题,但我不确定它们是否是最佳答案。 . . Struts 2 提供了 Spring 集成,该集成完全设计为允许您将依赖项无缝地注入到您自己的对象和核心框架对象中,例如操作。

    使用 Spring 插件,您可以简单地使用 XML 或注释配置您的 spring bean,仅此而已。 Spring 容器集成到框架的核心对象工厂中,这意味着您不必处理它;正是这个问题的代码中对容器的处理给 toksis 带来了问题。

    【讨论】:

    • 现在我正在使用插件.. 我也切换到 maven。我刚刚在 javabrains org 中研究了 java 核心,它使用了 spring.xml。我以为它可以在任何地方使用嘿嘿嘿。我现在所做的是在 applicationContext.xml 中使用依赖注入,到目前为止还可以:)
    • 如果你愿意,你可以在“任何地方”使用它,但是 struts 2 spring 插件会将来自 spring 容器的对象创建和注入与 struts 2 框架的对象创建集成在一起。 .所以你不必;)
    猜你喜欢
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多