如何在SpringApplicationRunListener中调用函数

时间:2017-01-04 19:33:48

标签: spring spring-boot

我有SpringApplicationRunListener这样的实现。

public class AppListener implements SpringApplicationRunListener {
    private long appStartTimestamp;
    private long appFinishTimestamp;

    public AppListener(SpringApplication application, String[] args) {
    }

    @Override
    public void started() {
        appStartTimestamp = System.currentTimeMillis();
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        //Not used.
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        //Not used.
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        //Not used.
    }

    @Override
    public void finished(ConfigurableApplicationContext context, Throwable exception) {
        appFinishTimestamp = System.currentTimeMillis();
    }

    public long getAppStartTimestamp() {
        return appStartTimestamp;
    }

    public long getAppFinishTimestamp() {
        return appFinishTimestamp;
    }
}

不使用任何静态方法或变量,如何调用getAppStartTimestamp()getAppFinishTimestamp()函数来获取两个变量?下面的代码不起作用,因为我初始化了AppListener的新实例。

AppListener appListener = new AppListener(new SpringApplicationBuilder(Application.class).build(), null);
appStartTimestamp = appListener.getAppStartTimestamp(); // 0
appFinishTimestamp = appListener.getAppFinishTimestamp(); // 0

我发现AppListener不能作为bean,因为它是在Spring启动bean管理之前调用的。有没有办法让正在运行的AppListener实例调用两个get函数?谢谢。

1 个答案:

答案 0 :(得分:3)

创建POJO以保存应用程序事件数据

@Component
public class AppEventData {

    private long appStartTimestamp;
    private long appFinishTimestamp;

    public long getAppStartTimestamp() {
        return appStartTimestamp;
    }

    public void setAppStartTimestamp(long appStartTimestamp) {
        this.appStartTimestamp = appStartTimestamp;
    }

    public long getAppFinishTimestamp() {
        return appFinishTimestamp;
    }

    public void setAppFinishTimestamp(long appFinishTimestamp) {
        this.appFinishTimestamp = appFinishTimestamp;
    }
}

现在更新你的侦听器类的finish方法,将开始和结束时间的值放在POJO中

public class AppListener implements SpringApplicationRunListener {
    private long appStartTimestamp;
    private long appFinishTimestamp;

    public AppListener(SpringApplication application, String[] args) {
        super();
    }

    @Override
    public void started() {
        appStartTimestamp = System.currentTimeMillis();
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        //Not used.
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        //Not used.
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        //Not used.
    }

    @Override
    public void finished(ConfigurableApplicationContext context, Throwable exception) {
        appFinishTimestamp = System.currentTimeMillis();
        AppEventData data = context.getBean(AppEventData.class);
        data.setAppStartTimestamp(appStartTimestamp);
        data.setAppFinishTimestamp(appFinishTimestamp);
    }

    public long getAppStartTimestamp() {
        return appStartTimestamp;
    }

    public long getAppFinishTimestamp() {
        return appFinishTimestamp;
    }
}

现在您可以从任何地方获取POJO中的数据,例如我向您展示主要方法。

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        AppEventData data = ctx.getBean(AppEventData.class);
        System.out.println(data.getAppStartTimestamp());
        System.out.println(data.getAppFinishTimestamp());
    }
}

已经完成......

  

不要忘记将监听器放在src/main/resources/META-INF/spring.factories中,如下所示,否则监听器将无法运行。

org.springframework.boot.SpringApplicationRunListener=com.app.AppListener