使用 Kotlin BeanDefinitionDsl 时未调用 @PostConstruct

BPS*_*BPS 5 spring kotlin spring-bean

当使用 Kotlin BeanDefinitionDsl 将 bean 添加到上下文时,似乎不会调用 @PostConstruct 方法。

这在我自己的项目中发生在我身上,但为了创建一种简单的方法来重现它,这就是我所做的。

  1. 我分叉了使用 Kotlin DSL 的 Spring 示例https://github.com/sdeleuze/spring-kotlin-function
  2. 我向 UserHandler 类添加了 @PostConstruct。(更多详细信息如下。)
  3. 我将结果推送到这里:https ://github.com/benjishults/spring-kotlin-functions

因此,您所需要做的就是分叉我的存储库并执行 gradle 运行。

我的问题是:

  1. 既然我将类作为 bean 引入,难道我不应该期望 @PostConstruct 被调用吗?
  2. 我是不是少了一步?
  3. 这是 Spring 的 bug 吗?

如果您不想拉取我的存储库,请参阅以下有关我所做操作的更多详细信息。我将其添加到 UserHandler 类中:

@PostConstruct
fun afterPropertiesSet() {
    System.out.println("AFTER PROPERTIES SET CALLED")
}
Run Code Online (Sandbox Code Playgroud)

以及导入和 Gradle 依赖项。

通过调用 beans DSL 中的 bean 方法将 UserHandler bean 拉入上下文,如下所示:

fun beans() = beans {
    bean<UserHandler>()
    // ...
}
Run Code Online (Sandbox Code Playgroud)

这被带入上下文:

beans().initialize(context)
Run Code Online (Sandbox Code Playgroud)

Séb*_*uze 5

GenericApplicationContextApplication类中实例化不支持开箱即用@PostContruct。为了使其正常工作,您应该在 Gradle 构建中使用AnnotationConfigApplicationContext并删除排除 for 。spring-aop