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

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

無法在多模塊 Gradle 應用程序中自動裝配 HttpServletRequest

無法在多模塊 Gradle 應用程序中自動裝配 HttpServletRequest

繁星點點滴滴 2023-10-13 17:24:03
我有多個由 Gradle 管理的 Spring Boot 微服務應用程序。有一個通用的 jar 和特定于域的 jar,組織如下:| common/ |-- common.jar | p/ |-- p-ingest.jar | g/ |-- g-ingest.jar我的常見 jar 有一個類,BaseRESTService自動裝配HttpServletRequest(我已經知道這是一個壞主意 - 不是我的代碼)。p-ingest.jar 導入 common,g-ingest jar 也是如此。geoalloc 運行沒有問題。但是 p-ingest(實際上是從 g-ingest 復制而來)不會運行。當上下文嘗試初始化時,p-ingest 出現以下異常:Field request in com.company.common.BaseRESTService required a bean of type 'javax.servlet.http.HttpServletRequest' that could not be found.我對 Spring 并不陌生,我了解組件掃描和自動裝配等所有內容,但我已經為此工作了 2 天,但我無法弄清楚發生了什么。我運行gradle dependencies了這兩個項目,樹是相同的。下面是p-ingest的啟動應用類:@SpringBootApplication@ComponentScan(        basePackageClasses = com.company.common.ConfigurationSearchCriteriaFactory.class,        basePackages = {"com.company.erd"})@EnableJpaRepositories(basePackages = "com.company.erd")@EntityScan(basePackages = {"com.company.erd"})public class PortfolioRiskIngestApplication implements ApplicationRunner {    private static final Log logger = LogFactory.getLog(IngestApplication.class);    @Autowired    private IngestService ingestService;    public PIngestApplication(PIngestService ingestService) {this.ingestService = ingestService;}    public static void main(String[] args) {        SpringProfileHelper.setProfileFromAwsEnvironment();        SpringApplication app = new SpringApplication(PIngestApplication.class);        app.setWebEnvironment(false);        app.run(args);    }    @PostConstruct      void started() {        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));      }    @Override    public void run(ApplicationArguments applicationArguments) {        // define the log info        LogInfo info = LogInfo.newInstance("run", new Object[] { StringUtility.arrayToString(applicationArguments.getSourceArgs()) });        // log entry        logger.info(info.entry());        ingestService.run(applicationArguments.getSourceArgs());        // log exit        logger.info(info.exit());    }}
查看完整描述

2 回答

?
MYYA

TA貢獻1868條經驗 獲得超4個贊

我發現了我的問題,這是一個菜鳥錯誤。出于懶惰,我對整個代碼庫進行了組件掃描,它找到了公共 jar 中僅適用于 REST 應用程序的控制器。組件僅掃描實際所需的包。



查看完整回答
反對 回復 2023-10-13
?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

您可以HttpServletRequest使用RequestContext


@Inject

private RequestContext requestContext;

并在方法中調用getRequest():


HttpServletRequest request = requestContext.getRequest();


查看完整回答
反對 回復 2023-10-13
  • 2 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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