亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Spring Boot 2 中檢索應用程序上下文

如何在 Spring Boot 2 中檢索應用程序上下文

子衿沉夜 2023-02-23 18:08:50
我ApplicationContextProvider定義了這個類以及MyApplication.java(運行應用程序的入口點):package com.company.my.app;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component;@Componentpublic class ApplicationContextProvider implements ApplicationContextAware {  private ApplicationContext applicationContext;  @Override  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {    this.applicationContext = applicationContext;  }  public ApplicationContext getContext() {    return applicationContext;  }}restapi擁有包含兩個類的包(Greeting只是一個保存數據的類):package com.company.my.app.restapi;import com.company.my.app.ApplicationContextProvider;import io.micrometer.core.instrument.Counter;import java.util.concurrent.atomic.AtomicLong;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.ApplicationContext;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class GreetingController {  private static final Logger LOG = LoggerFactory.getLogger(GreetingController.class);  private static final String template = "Hello, %s!";  private final AtomicLong counter = new AtomicLong();  @RequestMapping("/greeting")  public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {    ApplicationContextProvider acp = new ApplicationContextProvider();    ApplicationContext context = acp.getContext();    if (context == null) LOG.info("app context is NULL");    Counter bean = context.getBean(Counter.class);    bean.increment();    return new Greeting(counter.incrementAndGet(),        String.format(template, name));  }當我運行該應用程序并http://localhost:8081/greeting在我的瀏覽器中調用時,它會崩潰打印app context is NULL。如何獲取應用程序上下文?我需要它來檢索簡單的計數器 bean。
查看完整描述

1 回答

?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

你不需要上下文;有更好的方法。

ApplicationContextAware是許多舊版本 Spring 的產物,在許多現在的標準功能可用之前。在現代 Spring 中,如果您需要ApplicationContext,只需像注入任何其他 bean 一樣注入它。但是,您幾乎可以肯定不應該直接與它交互,尤其是對于getBean,應該將其替換為注入您得到的任何東西。


一般來說,當你需要一個 Spring bean 時,你應該將它聲明為構造函數參數。(如果你有多個構造函數,你需要用 注釋一個@Autowired,但如果只有一個構造函數,Spring 足夠聰明知道使用它。)如果你正在使用 Lombok,你可以使用 來@Value自動編寫構造函數,并且Groovy 和 Kotlin 具有相似的功能。


在您在這里展示的 Micrometer 的特定情況下,將單個指標聲明為 beans 是不常見的,因為它們是旨在應用于特定代碼路徑的細粒度工具。(某些服務可能有 10 個單獨的指標來跟蹤各種可能的情況。)相反,您注入MeterRegistry并選擇您需要的計數器或其他指標作為構造函數的一部分。在這里,您的控制器類應該如下所示。(我已經刪除了重復項AtomicLong,但如果有特定原因需要它,您可以按照顯示的那樣將其添加回去。)


@RestController

public class GreetingController {


  private static final Logger LOG = LoggerFactory.getLogger(GreetingController.class);


  private static final String template = "Hello, %s!";


  private final Counter counter;


  public GreetingController(MeterRegistry meterRegistry) {

    counter = meterRegistry.counter("my.counter");

  }



  @RequestMapping("/greeting")

  public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {


    counter.increment();

    long count = (long) counter.count();


    return new Greeting(count, String.format(template, name));

  }

}


查看完整回答
反對 回復 2023-02-23
  • 1 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號