本文介绍了我似乎无法实现Spring的生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Spring的应用程序上下文加载后立即进行一些操作.据我了解,我需要创建一个Lifecycle的实现并将一个bean引用放在上下文中.因此,在我的上下文中,我会遇到类似的情况:

I need something to happen immediately after Spring's application context is loaded. As I understand it, I need to create an implementation of Lifecycle and put a bean reference inside the context. So I have something like this in my context:

<bean id="mySpringLifecycle" class="com.my.project.MySpringLifecycle" />

该类看起来像这样:

public class MySpringLifecycle implements Lifecycle {

    @Override
    public void start() {
        System.out.println("The lifecycle has started.");
    }

    @Override
    public void stop() {
        return;
    }

    @Override
    public boolean isRunning() {
        return true;
    }
}

我没有收到任何错误,但是MySpringLifecycle从未打印出生命周期已经开始.",我的应用程序也可以正常启动.

I get no errors, but MySpringLifecycle never prints out "The lifecycle has started.", and my application starts just fine.

这是固定代码:

public class MySpringLifecycle implements SmartLifecycle {

    private volatile boolean isRunning = false;

    @Override
    public boolean isAutoStartup() {
        return true;
    }

    @Override
    public void stop(Runnable r) {
        System.out.println("STOPPED RUNNABLE!!!");
        isRunning = false;
    }

    @Override
    public void start() {
        System.out.println("STARTED!!!");
        isRunning = true;
    }

    @Override
    public void stop() {
        System.out.println("STOPPED!!!");
        isRunning = false;
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    @Override
    public int getPhase() {
        return 1;
    }
}

作为旁注,我还想提到一个我也可以使用的替代解决方案.我的web.xml中包含以下内容:

As a side note I also wanted to mention an alternative solution that I may use as well. I have the following in my web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

ContextLoaderListener上有一个名为contextInitialized的方法.因此,我所做的就是创建了自己的实现,并将其添加到了web.xml中.在我的实现中,我扩展了ContextLoaderListener,提供了contextInitialized方法的替代,该方法首先被称为super,然后执行我自己的功能.该操作仅执行一次,并且看起来运行良好.

The ContextLoaderListener has a method on it called contextInitialized. So what I did was created my own implementation and added that to the web.xml instead. In my implementation I extended ContextLoaderListener, provided an override for the contextInitialized method, called super for that method first, and then executed my own functionality. This gets executed only once, and appears to work well.

推荐答案

执行此操作的两种方法:

Two ways to do this:

  1. 使用SmartLifecycle代替Lifecycle,并确保从isAutoStartup()返回true.

  1. Implement SmartLifecycle instead of Lifecycle and make sure to return true from isAutoStartup().

实施ApplicationListener<ContextRefreshedEvent>.在这种情况下,只有一种方法要实现,而对于SmartLifecycle则只有6种方法.

Implement ApplicationListener<ContextRefreshedEvent>. In this case there is only one method to implement instead of 6 for SmartLifecycle.

这篇关于我似乎无法实现Spring的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:21