spring中@Configuration类的范围 [英] Scope of @Configuration Class in spring
问题描述
我们有 @Configuration
类,但它不包含任何 bean 定义,因为它是由某人编写的遗留类,我不想删除 @Configuration
注释,即使没有 bean 定义.不知何故,我想在 Web 应用程序的关闭挂钩上执行逻辑,但不知何故我无法从 contextDestroyed()
方法中的上下文中获取 bean,所以我想使用 @PreDestroy
在那个 @Configuration
类上.
We are having @Configuration
class, but it doesn't contain any bean definition and as it is a legacy class written by someone i don't want to remove the @Configuration
annotation even while there is no bean definition. Somehow i want to perform logic on shutdown hook of web application but somehow i am not able to get bean from context in the contextDestroyed()
method so i want to use @PreDestroy
on that @Configuration
class.
是否有人指定了 @Configuration
类的默认范围.因为我不希望如果作用域是非单一的,那么 @PreDestroy
会被一次又一次地调用.
Does any one specify what is the default scope of the @Configuration
class. As I don't want that if the scope is non-singleton that bean would the @PreDestroy
will be called again and again.
推荐答案
@Configuration
使用 @Component
进行元注释.这是它的 Scope 规则的来源.@Scope
注释的未指定值的组件(以及那些没有 Scope 注释的组件)默认为 Singleton Scope,可以在相应的 JavaDoc.
@Configuration
is meta-annotated with @Component
. This is where its Scope rules are derived from. @Scope
-annotated components that specify no value (and those without a Scope annotation for that matter) default to Singleton Scope as can be looked up in the respective JavaDoc.
这篇关于spring中@Configuration类的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!