[英]Spring Boot trying to resolve bean not in my xml
I want to test a class that simple shutdown the application: 我想测试一个简单地关闭应用程序的类:
@Component
public class ShutdownManager {
@Autowired
private ApplicationContext applicationcontext;
public int shutdown(int returnCode) {
return SpringApplication.exit(applicationcontext, () -> returnCode);
}
}
The test case I created is this: 我创建的测试用例是这样的:
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 contains only one bean: 我的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>
However, when I run this, it complains that: 但是,当我运行此命令时,它抱怨:
Field myOtherService in com.mycodebase.MyTask required a bean of type 'com.mycodebase.MyOtherService ' that could not be found.
The class MyTask is in the same package of the ShutdownManager which contains a field myOtherService which has an @Autowire annocation. MyTask类位于ShutdownManager的同一程序包中,该程序包包含具有@Autowire注释的字段myOtherService。 But it is not defined in my test xml which contains just one bean.
但这在我的仅包含一个bean的测试xml中未定义。
Can someone help me understand why it tries to resolve a bean that is not in the context? 有人可以帮我理解为什么它试图解析不在上下文中的bean吗?
It's doing that because that's how @SpringBootApplication
works. 这样做是因为
@SpringBootApplication
就是@SpringBootApplication
工作的。
@SpringBootApplication : @SpringBootApplication :
This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan .
这是一个方便注释,等效于声明@Configuration, @ EnableAutoConfiguration和@ComponentScan 。
If specific packages are not defined, scanning will occur from the package of the class that declares this annotation .
如果未定义特定的程序包, 则将从声明此批注的类的程序包中进行扫描 。
So everything in the same or a subpackage of ShutdownManagerTest is being picked up. 因此,将拾取ShutdownManagerTest的相同或子程序包中的所有内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.