gpt4 book ai didi

Java @Component 类和 @Configuration 类以及 AnnotationConfigApplicationContext

转载 作者:行者123 更新时间:2023-12-02 05:24:38 24 4
gpt4 key购买 nike

我知道 springs AnnotationConfigApplicationContext 不仅能够接受 @Configuration 类作为输入,还能够接受普通的 @Component 类和用 JSR 注释的类-330 元数据。

我在下面创建了 AppConfig.java,没有使用 @Configuration 注释。

public class AppConfig {

@Bean(name="sampleService")
public SampleService getSampleService(){
return new SampleService();
}

}

将该类作为我的 java 配置类传递给 AnnotationConfigApplicationContext,它接受并注册了我的服务 bean。

我对上面相同的 AppConfig 做了一些修改,如下所示。

  @Component
public class AppConfig {

@Bean(name="sampleService")
public SampleService getSampleService(){
return new SampleService();
}
}

将AppConfig传递给AnnotationConfigApplicationContext,它接受并注册了我的服务bean。

问题:

  1. AnnotationConfigApplicationContext类接受带有@Configuration的java配置类,没有@Configuration并且带有@Component注释,什么是@Component@Configuration 之间的区别?

  2. 为什么即使没有 @Configuration 注解也接受?

  3. 什么时候使用@Configuration,什么时候使用@Component作为java配置类?

最佳答案

@Component

Indicates that an annotated class is a "component".

也就是说,在启用组件扫描的上下文中,Spring 会为 @Component 注解类型生成 bean 定义。这些 bean 定义最终会变成 bean。

@Configuration ,它本身用

注释

Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime, [...]

因此,Spring 为其生成 bean 的任何 @Configuration 类型都充当 bean 的工厂。

javadoc of @Bean

@Bean methods may also be declared within classes that are not annotated with @Configuration. For example, bean methods may be declared in a @Component class or even in a plain old class. In such cases, a @Bean method will get processed in a so-called 'lite' mode.

Bean methods in lite mode will be treated as plain factory methods by the container (similar to factory-method declarations in XML), with scoping and lifecycle callbacks properly applied. The containing class remains unmodified in this case, and there are no unusual constraints for the containing class or the factory methods.

In contrast to the semantics for bean methods in @Configuration classes, 'inter-bean references' are not supported in lite mode. Instead, when one @Bean-method invokes another @Bean-method in lite mode, the invocation is a standard Java method invocation; Spring does not intercept the invocation via a CGLIB proxy. This is analogous to inter-@Transactional method calls where in proxy mode, Spring does not intercept the invocation — Spring does so only in AspectJ mode.

因此,@Bean 方法在 @Configuration 带注释的类中具有完整功能,而在 @Component 带注释的类中具有有限功能。

why it is Accepting even without @Configuration annotation?

这就是类的设计方式。 ApplicationContext 是一个BeanFactoryAnnotationConfigApplicationContext 只是提供了一种注册 bean 定义的额外方法。

When to use @Configuration, and when to use @Component as java config class?

这些目标确实完全不同。遵循 javadoc。当您需要设置 ApplicationContext 时,可以将 AnnotationConfigApplicationContext@Configuration 注解类一起使用。如果您只需要一个 bean,请使用 @Component 注释其类型。

关于Java @Component 类和 @Configuration 类以及 AnnotationConfigApplicationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26183635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com