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

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

Spring Boot 試圖解析不在我的 xml 中的 bean

Spring Boot 試圖解析不在我的 xml 中的 bean

波斯汪 2023-02-16 16:12:37
我想測試一個簡單關閉應用程序的類:@Componentpublic class ShutdownManager {    @Autowired    private ApplicationContext applicationcontext;    public int shutdown(int returnCode) {        return SpringApplication.exit(applicationcontext, () -> returnCode);    }}我創建的測試用例是這樣的:public class ShutdownManagerTest {    @Test    public void should_shutdown_gracefully() {        SpringApplication app = new SpringApplication(TestClass.class);        ConfigurableApplicationContext context = app.run();        ShutdownManager shutdownManager = (ShutdownManager)context.getBean("shutdownManager");        int result = shutdownManager.shutdown(10);        assertThat(result).isEqualTo(10);    }    @SpringBootApplication    @ImportResource("classpath:/core/shutdownmanagertest.xml")    private static class TestClass {        public TestClass() {        }        public static void main(String[] args) {        }    }}我的 shutdownmanagertest.xml 只包含一個 bean:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id="shutdownManager" class="com.mycodebase.ShutdownManager" /></beans>但是,當我運行它時,它會抱怨:Field myOtherService in com.mycodebase.MyTask required a bean of type 'com.mycodebase.MyOtherService ' that could not be found.MyTask 類位于 ShutdownManager 的同一個包中,其中包含一個字段 myOtherService,該字段具有 @Autowire 注釋。但它沒有在我的測試 xml 中定義,它只包含一個 bean。有人可以幫助我理解為什么它會嘗試解析不在上下文中的 bean 嗎?爪哇春天彈簧靴
查看完整描述

1 回答

?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

它這樣做是因為它是這樣@SpringBootApplication工作的。

@SpringBoot應用程序

這是一個方便的注釋,等效于聲明 @Configuration、@EnableAutoConfiguration 和@ComponentScan。

@組件掃描

如果未定義特定包,將從聲明此注解的類的包進行掃描。

因此,ShutdownManagerTest 的同一個包或子包中的所有內容都將被提取。


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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