我似乎无法理解这两个注释。我尝试阅读 javadocs 但仍然无法弄清楚。任何人都可以用简单的代码来解释这两个吗?
非常感谢。
最佳答案
您使用 @Configuration
作为配置 Spring bean 的基于 XML 的配置的替代品。因此,我们编写了一个类,而不是一个 xml 文件,并用 @Configuration
对其进行注释。并使用 @Bean
定义其中的 bean方法上的注释。
最后你使用 AnnotationConfigApplicationContext
注册 @Configuration
类,因此 spring 管理定义的 bean。您可以在 Spring Configuration Documentaion 找到小示例.
从上面的链接引用
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
是一个注解,它将依赖项注入(inject)到不受 Spring 使用 aspectj 库管理的对象中。即,您仍然使用普通的实例化方式 new
运算符来创建对象,但 spring 会自动为您将依赖项注入(inject)该对象。
关于Spring 框架 @Configurable 与 @Configuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46362751/