【问题标题】:How do I execute a method in Spring when the Server is ready?当服务器准备好时,如何在 Spring 中执行方法?
【发布时间】:2015-07-01 16:55:10
【问题描述】:

我有以下问题:我正在将 Spring-Boot 用于一个基于 Web 的小型私有项目,并且我希望 Spring 在启动时调用 Web 服务。开始我的意思是“当我的应用程序准备好处理请求时”。

我已经尝试过实现ApplicationListenerContextRefreshedEvent>,但它不起作用,因为事件发生在早期(即在嵌入式服务器准备好处理请求之前)。 this question 中提到的选项也没有解决这个问题。

我现在的问题是:在服务器完成启动并准备好处理请求后,是否有可能告诉 Spring 执行某些操作?

编辑(回应丹尼尔的回答):

问题是我需要一些注入的属性来进行 web 服务调用,并且由于注入静态值在 spring 中不起作用,这种方法是没有选择的。

我的听众,这是我想要的,只是有点太早了,看起来像这样:



@Component
public class StartupListener implements ApplicationListener{


    @Autowired
    private URLProvider urlProvider;
    @Value("${server.port}")
    private int port;
    @Value("${project.name}")
    private String projectName;

    @Override
    public final void onApplicationEvent(ContextRefreshedEvent event) {
        RestTemplate template = new RestTemplate();
        String url = uriProvider.getWebserviceUrl(this.projectName);
        template.put(url, null);
    }

}

第二次编辑:

虽然this 问题解决了一个非常相似的问题,但我似乎无法注入对象,因为它需要有一个形式为 (org.springframework.boot.SpringApplication, [Ljava.lang.字符串;)。

此外,还希望无需创建 spring.factories 文件而是使用注释来解决它。

【问题讨论】:

  • 嗯,您的应用程序是使用 Spring MVC 作为其 Web 部件还是其他框架或根本没有框架?
  • 我正在使用 WebMVC 和 Vaadin 作为 UI 框架。

标签: java spring spring-boot


【解决方案1】:

如果我了解您的问题所在,您可以在应用程序主服务器启动后立即调用该 Web 服务。

public static void main(String[] args) {

        new SpringApplication(Application.class).run(args);
        //call the webservice for you to handle...
    }

我不确定这是否是你想要的......

【讨论】:

  • 问题是,据我所知,我需要一些注入的属性来进行 Web 服务调用,而静态注入并不能真正起作用。
  • 你能举个例子吗?从 spring 文档中,您可以执行如下请求: public static void main(String args[]) { RestTemplate restTemplate = new RestTemplate(); Page page = restTemplate.getForObject("graph.facebook.com/pivotalsoftware", Page.class); System.out.println("名称:" + page.getName()); System.out.println("关于:" + page.getAbout()); System.out.println("电话:" + page.getPhone()); System.out.println("网站:" + page.getWebsite()); }
  • 请看我的编辑(不幸的是,不可能硬连线所有变量)
【解决方案2】:

在您的组件中,您可以使用@PostConstruct 注释。例如

@Component
public class StartupListener {


    @Autowired
    private URLProvider urlProvider;
    @Value("${server.port}")
    private int port;
    @Value("${project.name}")
    private String projectName;

    @PostConstruct
    public final void init() {
        RestTemplate template = new RestTemplate();
        String url = uriProvider.getWebserviceUrl(this.projectName);
        template.put(url, null);
    }

}

这将在 bean 初始化并发生自动装配后触发。

【讨论】:

  • 不幸的是,这不起作用,因为调用此方法时应用程序未完全启动。
  • 您能解释一下“未完全开始”是什么意思吗?
  • 完整启动意味着(如问题中所述)服务器已准备好处理请求。因为应用程序将从网络服务中回调。
【解决方案3】:
  @Component
  public class StartUp implements ApplicationListener<WebServerInitializedEvent> {

  private WebClient webClient;

  @Override
  public void onApplicationEvent(WebServerInitializedEvent event) {

    String baseUrl = "http://url.com"
    webClient = WebClient.create(baseUrl);
    executeRestCall(baseUrl+"/info");
  }

  void executeRestCall(String uri) {
    try {
      webClient.get()
          .uri(uri)
          .exchange()
          .block();
    } catch (Exception e) {
      log.error("Request failed for url - {}",uri, e);
    }
  }}

【讨论】:

    猜你喜欢
    • 2019-10-22
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2020-03-15
    • 1970-01-01
    • 2020-01-20
    相关资源
    最近更新 更多