We use a Bean factory function to allow injection of Logger objects into our Beans. This looks similar to what Simar Paul Singh describes in his article "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
)
}
Today I attempted to convert this declaration into a Bean declaration using Springs functional bean definition DSL. However I did not succeed to get a hold of the InjectionPoint used to retrieve the class the Logger is injected into.
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
)
}
}
The above results in an exception along the lines of:
NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.beans.factory.InjectionPoint' available
Is there a way to retrieve the InjectionPoint or at least the class to provide a behaviour similar to the Bean declaration without DSL?
A minimal reproduction repository can be found on GitHub.
Run ./gradlew bootRun or .\gradlew.bat bootRun on Windows to reproduce the error.
I've opened issue #27738 on the GitHub repository of Spring.