简体   繁体   English

如何重置Spring托管Bean

[英]How to reset a Spring managed Bean

NOTE: This issue may or may not be Vaadin related, depending on wether there is a "better" solution to "resetting" the bean or not. 注意: 这个问题可能与Vaadin有关,也可能不是,这取决于是否有“更好”的解决方案“重置”bean。

Background scenario 背景情景

I am building a wizard for entering some values that, when finished, is sent to a table (using Vaadin and the add-on "Wizards for Vaadin"). 我正在构建一个向导来输入一些值,这些值在完成后会被发送到一个表(使用Vaadin和附加的“Wizards for Vaadin”)。

The add-on does not provide a way to reset the Wizard (ie go back to step 1) without a forced call to the current steps (overridden) onAdvance() and onBack() methods, which will return false in some of my steps because I'm using logic in those methods in case the use for example haven't filled in all required data. 加载项没有提供重置向导的方法(即返回到步骤1),而没有强制调用onAdvance()和onBack()方法的当前步骤(重写),这些方法在我的一些步骤中将返回false因为我在那些方法中使用逻辑,以防例如使用没有填写所有必需的数据。

I cannot simple create a new instance of the Wizard, because I'm using Spring to manage this @Component. 我不能简单地创建一个新的Wizard实例,因为我使用Spring来管理这个@Component。

So, this leaves me with actually resetting the bean in order to reset the wizard correctly. 所以,这让我实际上重置了bean,以便正确地重置向导。

My question is 我的问题是

How do I "reset" a Spring managed Bean (@Component)? 如何“重置”Spring托管Bean(@Component)? I should add that this Bean also has some dependencies injected to it. 我应该补充一点,这个Bean也有一些依赖注入它。

or... (for the Vaadin people): 或者......(对于瓦丁人来说):

Is there another way of resetting this wizard other than creating a new Wizard? 除了创建新向导之外,还有另一种重置此向导的方法吗?

Some code 一些代码

@Component
@Scope("session")
public class MyWizard extends Wizard {

    @Inject
    private MyWizardContainer myWizardContainer;
    @Inject
    private MyService myService;
    @Inject
    private MyWizardStep1 myWizardStep1;
    @Inject
    private MyWizardStep2 myWizardStep2;
    @Inject
    private MyWizardStep3 myWizardStep3;
    @Inject
    private MyMainTableContainer myMainTableContainer;

    final static Logger logger = LoggerFactory.getLogger(MyWizard.class);

    private static final long serialVersionUID = 1L;

    public MyWizard() {
        setupListener();
    }

    public void addSteps() {
        this.addStep(myWizardStep1);
        this.addStep(myWizardStep2);
        this.addStep(myWizardStep3);
        this.mainLayout.setComponentAlignment(this.footer, Alignment.BOTTOM_LEFT);
    }

    private void setupListener() {

        this.addListener(new WizardProgressListener() {

            @Override
            public void wizardCompleted(WizardCompletedEvent event) {
                endWizard("Wizard Finished Successfully!");
            }

            @Override
            public void wizardCancelled(WizardCancelledEvent event) {
                endWizard("Wizard Cancelled!");
            }

            @Override
            public void stepSetChanged(WizardStepSetChangedEvent event) {
                // TODO Auto-generated method stub
            }

            @Override
            public void activeStepChanged(WizardStepActivationEvent event) {
                // TODO Auto-generated method stub
            }
        });
    }

    private void resetWizard() {
        myWizardContainer.removeAll(); //here I'm simply resetting all data that the user generated thus far in the wizard
        this.activateStep(myWizardStep1); //this will not work, as some steps will not always return true on onBack() and/or onAdvance()
    }

    private void endWizard(String message) {
        resetWizard();
        this.setVisible(false);
        Notification.show(message);
    }
}

SpringVaadinIntegration, that you probably use, does not require all elements to be @Components, only UI has to be annotated. 您可能使用的SpringVaadinIntegration不要求所有元素都是@Components,只需要注释UI。

Your wizard and it's steps should not be components in this case, if you really need to inject dependencies into them you can use @Configurable annotation, which allowes to inject dependencies to classes not managed by Spring (some more configuration needed to make it work). 在这种情况下,你的向导及其步骤不应该是组件,如果你真的需要向它们注入依赖项,你可以使用@Configurable注释,它允许将依赖项注入不由Spring管理的类(需要一些更多的配置来使它工作) 。 Just create wizard and steps as new objects when you need them. 只需在需要时创建向导和步骤作为新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM