0

我想要做的是调用一个内部类中的方法,当我尝试从其他类调用它时问题就开始了。假设我有一个 DlgSendSAP 类,它有一个名为 LongTask 的内部类,其中我有另一个名为 ActualTask​​ 的内部类,它有我想要调用的方法。这里有什么问题?我已经阅读了很多 Hibernate 和 Spring,并且我知道基础知识,但这让我发疯,我不知道是否是 IoC 的问题。任何帮助将不胜感激。

DlgSendSAP 类

public class DlgSendSAP {
  ...
  dlgCargarTxt = ((DlgCargarTxt) SpringContext.getContext().getBean(DlgCargarTxt.class));
                    try {
                        dlgCargarTxt.ejecutarTask();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
}

DlgCargarTxt 类

public class DlgCargarTxt {
  private LongTask.ActualTask actualTask;
  ...
     public void ejecutarTask() throws FileNotFoundException, IOException {
      actualTask = ((LongTask.ActualTask) SpringContext.getContext().getBean(LongTask.ActualTask.class));
      actualTask.executeTask();
    } 
  class LongTask {
  ...
    class ActualTask {
      ...
      public void executeTask() {
      ...
      }
    }
  }
}

当调用内部类 ActualTask​​ 中的 bean 时出现错误。

org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [dunosusa.pos.view.LongTask] is defined: expected single bean but found 0: 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:271)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
    at dunosusa.pos.view.DlgCargarTxt.ejecutarTask(DlgCargarTxt.java:135)
    at dunosusa.pos.view.admin.DlgSendSAP.escogerArchivos(DlgSendSAP.java:63)
    at dunosusa.pos.view.admin.DlgSendSAP.<init>(DlgSendSAP.java:49)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:74)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:265)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
    at dunosusa.pos.controller.AccionController$ActionPerformed.actionPerformed(AccionController.java:392)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1223)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1264)
    at java.awt.Component.processMouseEvent(Component.java:6263)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6028)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4630)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
4

1 回答 1

0

通过使用 SpringContext.getContext().getBean() - 您正在搜索 Spring 的 bean 池以获取您正在寻找的类。为了使 bean 在 Spring 的 bean 池中,您还必须将内部类定义为 bean。

我看到您正在使用 XML 进行配置。你应该像这样配置你的内部类:

<bean id="inner" class="innerbeans.Outer$Inner">

但是,同样,这不是很好的做法。为什么不直接提取类并使其成为自己的?

于 2013-03-05T20:19:29.723 回答