带有AnnotationConfigApplicationContext的Java @Component类和@Configuration类 [英] Java @Component class and @Configuration class with AnnotationConfigApplicationContext
问题描述
我知道弹簧 AnnotationConfigApplicationContext
不仅可以接受 @Configuration
类作为输入,还可以接受普通的 @Component
类和带有JSR-330元数据注释的类。
I know springs AnnotationConfigApplicationContext
is capable of accepting not only @Configuration
classes as input but also plain @Component
classes and classes annotated with JSR-330 metadata.
我在下面创建了没有@Configuration注释的AppConfig.java。 / p>
I have created AppConfig.java below without @Configuration annotation.
public class AppConfig {
@Bean(name="sampleService")
public SampleService getSampleService(){
return new SampleService();
}
}
将该类作为我的java配置传递 AnnotationConfigApplicationContext
类,它接受并注册了我的服务bean。
Passed this class as my java config class to AnnotationConfigApplicationContext
, it accepted and registered my service beans.
我对同一个AppConfig进行了如下修改
I did some modification on above same AppConfig like below.
@Component
public class AppConfig {
@Bean(name="sampleService")
public SampleService getSampleService(){
return new SampleService();
}
}
将AppConfig传递给AnnotationConfigApplicationContext,它接受并注册了我的服务
passed AppConfig to AnnotationConfigApplicationContext, it accepted and registered my service beans.
问题:
-
AnnotationConfigApplicationContext
类接受带有@Configuration
,不带有@Configuration
和带有@Component批注,@Component
和@Configuration
有什么区别?
AnnotationConfigApplicationContext
class is accepting the java config class with@Configuration
, without@Configuration
and with @Component annotations, what is the difference between@Component
and@Configuration
?
为什么没有 @Configuration
批注也能接受?
Why is it Accepting even without @Configuration
annotation?
何时使用 @Configuration
,何时使用@Component作为Java配置类?
When to use @Configuration
, and when to use @Component as java config class?
推荐答案
表示带注释的类是
Indicates that an annotated class is a "component".
也就是说,在启用了组件扫描的上下文中,Spring为 @Component
带注释的类型。这些bean定义最终变成了bean。
That is, in a context where component scanning is enabled, Spring generates bean definitions for @Component
annotated types. These bean definitions end up being turned into beans.
@Configuration
,其本身带有
@Configuration
, which is itself annotated with
表示一个类声明了一个或多个
@Bean
方法,并可能由
处理Spring容器在运行时为这些bean生成bean定义和
服务请求,[...]
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, [...]
@Configuration
类型(Spring会为此生成一个bean)充当bean的工厂。
So any @Configuration
type, for which Spring generates a bean, acts as a factory for beans.
@Bean
方法也可以在不是
的类中声明用@Configuration
注释。例如,bean方法可以是在@Component
类中甚至在普通的旧类中声明的
。在这种
情况下,@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方法将被
容器视为普通工厂方法(类似于XML中的工厂方法声明),并正确应用
作用域和生命周期回调。在这种情况下,包含类
保持不变,并且包含类或工厂方法没有
异常约束。
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.
在 @Configuration
类中的bean方法的语义中,精简模式不支持 bean间引用。
相反,当一个 @Bean
方法调用精简版
中的另一个 @Bean
方法时模式,该调用是标准的Java方法调用; Spring不会
不会通过CGLIB代理拦截调用。这类似于
inter- @Transactional
方法调用,其中在代理模式下,Spring不会
拦截调用— Spring仅在AspectJ模式下会如此
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
带注释的类中的功能受限。
So @Bean
methods have full functionality in @Configuration
annotated classes and limited functionality in @Component
annotated classes.
为什么即使没有@Configuration注释也可以接受?
why it is Accepting even without @Configuration annotation?
这就是设计类的方式。 ApplicationContext
是 BeanFactory
。 AnnotationConfigApplicationContext
只是提供了一种额外的方法来注册bean定义。
That's how the class is designed. An ApplicationContext
is a BeanFactory
. AnnotationConfigApplicationContext
simply offers an extra way to register a bean definition.
何时使用@Configuration,以及何时使用@Component作为java config类?
When to use @Configuration, and when to use @Component as java config class?
这些确实是完全独立的目标。遵循javadoc。当您需要设置 ApplicationContext
时,可以将 AnnotationConfigApplicationContext
与 @Configuration
带注释的类。如果您只需要一个bean,请使用 @Component
注释其类型。
These really completely separate goals. Follow the javadoc. When you need to setup an ApplicationContext
, you can use an AnnotationConfigApplicationContext
with a @Configuration
annotated class. If you simply need a bean, annotate its type with @Component
.
这篇关于带有AnnotationConfigApplicationContext的Java @Component类和@Configuration类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!