[英]Does using @Import annotation create all beans defined in imported file?
@Configuration
@Import(B.class)
class A {
@Bean
public X x(Y y) {
return new X(y);
}
}
@Configuration
class B {
@Bean
public Y y() {
return new Y();
}
// And some other beans are defined
}
Will all beans defined in B.java
be instantiated when bean x
is instantiated?当bean
x
被实例化时, B.java
定义的所有bean B.java
被实例化吗? Since the file B.java
is imported by A.java
.由于文件
B.java
是由A.java
导入的。
A simple test to visualize your answer:一个简单的测试来可视化你的答案:
class ATest {
private static final Logger LOGGER = LoggerFactory.getLogger(A.class);
private ApplicationContext ctx;
@BeforeEach
void setUp() {
ctx = new AnnotationConfigApplicationContext(A.class);
}
@Test
void test() {
for (String beanName: ctx.getBeanDefinitionNames()) {
LOGGER.info("BEAN: " + beanName);
}
}
}
Output:输出:
[main] INFO be.dog.d.steven.A - BEAN: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
[main] INFO be.dog.d.steven.A - BEAN: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
[main] INFO be.dog.d.steven.A - BEAN: org.springframework.context.event.internalEventListenerProcessor
[main] INFO be.dog.d.steven.A - BEAN: org.springframework.context.event.internalEventListenerFactory
[main] INFO be.dog.d.steven.A - BEAN: a
[main] INFO be.dog.d.steven.A - BEAN: org.company.B
[main] INFO be.dog.d.steven.A - BEAN: y
[main] INFO be.dog.d.steven.A - BEAN: x
As you can see, @Import
did make the application context include all beans in class B as expected.如您所见,
@Import
确实使应用程序上下文按预期包含类 B 中的所有 bean。
The first 4 beans are always created, even if you use just one empty class annotated with @Configuration
to build the application context.前 4 个 bean 始终被创建,即使您只使用一个用
@Configuration
注释的空类来构建应用程序上下文。 The fifth bean that will always be created is the annotated class used to create the context.将始终创建的第五个 bean 是用于创建上下文的带注释的类。
Although this works fine, it is cleaner to have your application do a @ComponentScan(basePackages = "org.company")
in a certain package or folder.尽管这工作正常,但让您的应用程序在某个包或文件夹中执行
@ComponentScan(basePackages = "org.company")
会更清晰。 This way you do not always have to add new components to the @Import
annotation.这样您就不必总是向
@Import
注释添加新组件。 (You might also not want to scan the whole project when you are working on a bigger project, rather just folders containing actual components.) (在处理更大的项目时,您可能也不想扫描整个项目,而只想扫描包含实际组件的文件夹。)
You can however use @Import
on test classes to only load an isolated part of the context for that test.但是,您可以在测试类上使用
@Import
来仅为该测试加载上下文的隔离部分。
You can also set the logging level of the Spring Framework to DEBUG in the application.properties :您还可以在application.properties中将 Spring Framework 的日志记录级别设置为 DEBUG:
logging.level.org.springframework=DEBUG
This way you can also check the logs of your application to see what beans get created.通过这种方式,您还可以检查应用程序的日志以查看创建了哪些 bean。
First I ran the sample that is posted by you.首先,我运行了您发布的示例。 Then I tested one sample with following variation,
然后我测试了一个具有以下变化的样本,
@Configuration
@Import(B.class)
class A {
@Bean
public X x(Y y) {
return new X(y);
}
}
And then the last variation as following,然后最后的变化如下,
@Slf4j
@Configuration
class B {
@Bean
public Y y() {
log.info("Creating bean Y in B {}", System.currentTimeMillis());
return new Y();
}
@Bean
public Z z(){
log.info("Creating bean Z in B {}", System.currentTimeMillis());
return new Z();
}
// And some other beans are defined
}
Here is what I inferred from my observations,这是我从我的观察中推断出来的,
@Bean
, bean is declared from a class which is annotated with either @Component
, @Configuration
or @Service
and your component class is part of your declared package in @ComponentScan
, then Spring will always register your bean.@Bean
,bean 始终是从类中声明的,该类使用@Service
Component、 @Service
@Configuration
或@Service
进行@Component
,并且您的组件类是您在@ComponentScan
声明的包的一部分,然后 Spring 将总是注册你的bean。 I can safely say that @Import has very little to do with bean creation. @Import is just used to group your bean definition source as I found here on Baeldung, @Import @Import 只是用来对你的 bean 定义源进行分组,正如我在 Baeldung 上找到的那样, @ Import
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.