【发布时间】:2021-12-28 16:25:42
【问题描述】:
我们使用 Bean 工厂函数来允许将 Logger 对象注入到我们的 Bean 中。 这看起来类似于 Simar Paul Singh 在他的文章“Inject loggers in Spring”中描述的内容
import org.slf4j.*
import org.springframework.beans.factory.InjectionPoint
import org.springframework.context.annotation.*
@Bean
@Scope("prototype")
fun logger(injectionPoint: InjectionPoint): Logger {
return LoggerFactory.getLogger(
injectionPoint.methodParameter?.containingClass // constructor
?: injectionPoint.field?.declaringClass // or field injection
)
}
今天我尝试使用Springs functional bean definition DSL 将此声明转换为 Bean 声明。但是,我没有成功获取用于检索 Logger 注入的类的InjectionPoint。
import org.slf4j.*
import org.springframework.beans.factory.InjectionPoint
import org.springframework.context.support.beans
import org.springframework.context.support.BeanDefinitionDsl.Scope.PROTOTYPE
fun beans() = beans {
bean(scope = PROTOTYPE) {
val injectionPoint = ref<InjectionPoint>()
LoggerFactory.getLogger(
injectionPoint.methodParameter?.containingClass // constructor
?: injectionPoint.field?.declaringClass // or field injection
)
}
}
上述结果导致异常:
NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.beans.factory.InjectionPoint' available
有没有办法检索InjectionPoint 或至少是类以提供类似于没有DSL 的Bean 声明的行为?
可以在GitHub 上找到最小的复制存储库。
在 Windows 上运行 ./gradlew bootRun 或 .\gradlew.bat bootRun 以重现错误。
我在 Spring 的 GitHub 存储库上打开了issue #27738。
【问题讨论】:
-
你能分享你的整个代码或 GitHub 链接吗?
-
@Raj 我向 GitHub 添加了一个复制最少的存储库。请参阅问题的最后一段。
-
@xerx593 感谢您的回复。我可能很快会在 Spring 的 GitHub 存储库中提交一个问题。我知道并期待着“春福”的进展,现在悲哀地停止了。
-
..我认为,这些家伙(实现 DSL 支持)只是忘记了这一点(also in the Test)The documentation 也需要一些努力。 (死链接,不正确/复制和粘贴)
标签: java spring spring-boot kotlin dsl