Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Boot试图解析不在我的xml中的bean_Java_Spring_Spring Boot - Fatal编程技术网

Java Spring Boot试图解析不在我的xml中的bean

Java Spring Boot试图解析不在我的xml中的bean,java,spring,spring-boot,Java,Spring,Spring Boot,我想测试一个简单关闭应用程序的类: @Component public class ShutdownManager { @Autowired private ApplicationContext applicationcontext; public int shutdown(int returnCode) { return SpringApplication.exit(applicationcontext, () -> returnCode);

我想测试一个简单关闭应用程序的类:

@Component
public 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) {
        }
    }
}
My 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>
类MyTask位于ShutdownManager的同一个包中,该包包含一个字段myOtherService,该字段具有@Autowire注释位置。但它并没有在我的测试xml中定义,它只包含一个bean


有人能帮我理解为什么它试图解析不在上下文中的bean吗?

它这样做是因为
@SpringBootApplication
就是这样工作的

:

这是一个方便的注释,相当于声明@Configuration、@EnableAutoConfiguration和@ComponentScan

如果未定义特定的包,将从声明此注释的类的包进行扫描


因此,ShutdownManagerTest的相同或子包中的所有内容都会被提取。

这样做是因为
@SpringBootApplication
就是这样工作的

:

这是一个方便的注释,相当于声明@Configuration、@EnableAutoConfiguration和@ComponentScan

如果未定义特定的包,将从声明此注释的类的包进行扫描


因此,ShutdownManagerTest的相同或子包中的所有内容都将被提取。

您需要添加更多代码才能解决此问题您需要添加更多代码才能解决此问题
Field myOtherService in com.mycodebase.MyTask required a bean of type 'com.mycodebase.MyOtherService ' that could not be found.