Skip to content

Provide InjectionPoint inside Spring functional bean definition DSL #27738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
EndzeitBegins opened this issue Nov 26, 2021 · 1 comment
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@EndzeitBegins
Copy link

EndzeitBegins commented Nov 26, 2021

Meta information about the class or method that is requesting an instance of a managed bean
should be available when using Spring's functional bean definition DSL.

This might be the corresponding InjectionPoint or at least the class name of the requesting instance.


One can use a Bean factory function to allow injection of Logger objects into ones Beans, e.g.:

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
  )
}

This is not possible with the functional bean definition DSL in Kotlin.
Neither of the two examples down below work in the most recent version of Spring, unfortunately.

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
        )
    }

    bean(scope = PROTOTYPE) { injectionPoint: InjectionPoint ->
        LoggerFactory.getLogger(
            injectionPoint.methodParameter?.containingClass // constructor
                ?: injectionPoint.field?.declaringClass // or field injection
        )
    }
}

See also this unanswered question of StackOverflow.
The injection into the factory functions seems to be possible since SPR-14033 / #18605.
A minimal reproduction repository can be found on GitHub.
Run ./gradlew bootRun or .\gradlew.bat bootRun on Windows to reproduce the error.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 26, 2021
@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 29, 2021
@sdeleuze sdeleuze self-assigned this Oct 5, 2023
@sdeleuze
Copy link
Contributor

sdeleuze commented Oct 5, 2023

Sorry for the delay. I think I am not in favor of exposing InjectionPoint support in the functional bean DSL since it looks to me more tied to the annotation and reflection based injection, not the functional one. Mixing both would be potentially confusing.

@sdeleuze sdeleuze closed this as completed Oct 5, 2023
@sdeleuze sdeleuze added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

4 participants