Spring 框架@Configurable 与@Configuration
Spring framework @Configurable vs @Configuration
我似乎无法理解这两个注释。我已经尝试阅读 javadoc,但仍然无法弄清楚。任何人都可以用简单的代码帮助解释这两个吗?
非常感谢。
@Configuration 是基于 Java 的配置机制的核心,并提供了基于 XML 的配置的替代方案。
@Configuration 类 就像常规的@Components 类,除了用@Bean 注释的方法用于工厂bean。
您使用 @Configuration
替代基于 XML 的配置来配置 spring bean。因此,我们编写了一个 class 文件,而不是 xml 文件,并用 @Configuration
对其进行注释,并在方法上使用 @Bean
注释定义其中的 bean。
最后你使用 AnnotationConfigApplicationContext
注册这个 @Configuration
class 从而 spring 管理定义的 bean。您可以在 Spring Configuration Documentaion.
找到小示例
引自上文link
It is just another way of 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.
而@Configurable
是一个注释,它使用aspectj库将依赖注入到不受Spring管理的对象中。也就是说,您仍然使用带有普通 new
运算符的旧实例化方法来创建对象,但是 spring 会自动为您将依赖项注入该对象。
我似乎无法理解这两个注释。我已经尝试阅读 javadoc,但仍然无法弄清楚。任何人都可以用简单的代码帮助解释这两个吗? 非常感谢。
@Configuration 是基于 Java 的配置机制的核心,并提供了基于 XML 的配置的替代方案。 @Configuration 类 就像常规的@Components 类,除了用@Bean 注释的方法用于工厂bean。
您使用 @Configuration
替代基于 XML 的配置来配置 spring bean。因此,我们编写了一个 class 文件,而不是 xml 文件,并用 @Configuration
对其进行注释,并在方法上使用 @Bean
注释定义其中的 bean。
最后你使用 AnnotationConfigApplicationContext
注册这个 @Configuration
class 从而 spring 管理定义的 bean。您可以在 Spring Configuration Documentaion.
引自上文link
It is just another way of 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.
而@Configurable
是一个注释,它使用aspectj库将依赖注入到不受Spring管理的对象中。也就是说,您仍然使用带有普通 new
运算符的旧实例化方法来创建对象,但是 spring 会自动为您将依赖项注入该对象。