Open ArnauAregall opened 1 year ago
As far as I could test forking the project, and adding kotlin.Function
as supported type in FunctionTypeUtils
, I believe it will not be enough in terms of developer experience.
I have a working approach but it requires to use org.springframework.cloud.function.context.config.KotlinLambdaToFunctionAutoConfiguration.KotlinFunctionWrapper
to build the target function, which is particularly ugly as it is a class intended for Spring Boot bean definition approach.
@SpringBootConfiguration
class DemoKotlinFunctionalApplication : ApplicationContextInitializer<GenericApplicationContext> {
fun uppercase(): (String) -> String = String::uppercase
override fun initialize(context: GenericApplicationContext) {
val target = KotlinLambdaToFunctionAutoConfiguration.KotlinFunctionWrapper(uppercase())
target.setName("uppercase")
target.setBeanFactory(context.beanFactory)
context.registerBean("demo", FunctionRegistration::class.java, Supplier {
FunctionRegistration(target)
.type(FunctionTypeUtils.functionType(String::class.java, String::class.java))
})
}
}
fun main(args: Array<String>) {
FunctionalSpringApplication.run(DemoKotlinFunctionalApplication::class.java, *args)
}
Maybe KotlinFunctionWrapper
could be externalized somehow from the auto configuration class.
If you have any idea/insights I could open a PR .
Any update on this? Because this became major issue in our project after upgrading to kotlin 2.x.x and I had to rewrite lots of beans to use java functional interface.
Versions: spring boot 3.0.13 spring cloud dependencies 2022.0.5
Any update on this? Because this became major issue in our project after upgrading to kotlin 2.x.x and I had to rewrite lots of beans to use java functional interface.
Versions: spring boot 3.0.13 spring cloud dependencies 2022.0.5
As OP, I discarded the functional bean definition approach when using Kotlin due this.
Instead, I spent effort on generating GraalVM native images with traditional bean definition, as my need was a fast startup.
As OP, I discarded the functional bean definition approach when using Kotlin due this.
Instead, I spent effort on generating GraalVM native images with traditional bean definition, as my need was a fast startup.
Yeah, well, that's not very convenient in terms of interoperability between kotlin and java.
I'd expect to get FunctionTypeUtils
fixed to be a kotlin-friendly as well.
Why/How did you find extending the condition not sufficient?
Even if kotlin.Function is added as a supported type, functional bean definition would still need to make use of SCF Boot Autoconfig internal classes, which is a bit contradictory for me.
I coincide that would be nice to see this refactored, although personally I don't plan to contribute any longer to this topic specifically., at least in the short term.
Describe the bug
I am trying to follow the reference documentation section about registering functions with the Functional Bean definition approach, but using Kotlin.
Probably I am doing something wrong, but I could not find a way to define the
FunctionRegistration#type
either than usingorg.springframework.cloud.function.context.catalog.FunctionTypeUtils#functionType()
, but seems it does not support Kotlin functions.Versions:
spring-cloud-function-kotlin:4.0.3
)Sample
The Java version works perfectly in the same project with the same approach + same dependencies.