Today I discovered that in Kotlin Spring (JVM) when I define a `@Service` with a method that declare...
m
Today I discovered that in Kotlin Spring (JVM) when I define a
@Service
with a method that declares default parameter values and then call the service from another
@Component
not supplying a value, there is a NoSuchMethodError thrown. Apparently Spring creates a (Java) proxy for the service which does not support default parameter values (not supported in Java). Is there a solution for this problem?
r
Spring doesn't always create proxies. I'm using
@Service
annotated components with methods with default parameters without problems.
There are likely specific cases when proxies are used, unfortunately I don't know what they are.
m
Usually AOP triggers the use of Proxies. I don‘t need them, but the annoying point is that the code compiles and then fails at runtime …
b
code compiles and then fails at runtime
That is the major downside of using a framework like spring where it builds the dependency graph at runtime.
👍 2
m
It‘s not so much a dependency problem, it’s the implicit code generation that is enabled elsewhere (not in the code it affects).
t
Transactional annotation is creating proxies I guess
w
I assume if you use functional bean declaration, it won't create proxies, but then you can't use any AOP annotations either
c
One way to overcome this is to create a Configuration annoted class with Bean annotated method to create the service yourself.
w
Oh I didn't realize that prevents the proxy
m
@corneil sure, there are workarounds - but it kind of spoils the experience. For my project the decision will be to move away from Spring. I’ve always wanted to try Ktor 😎
c
If you want the bean to support the transactions like with the Transactional annotation you can use TransactionProxyFactoryBean
m
I don’t need or want any transaction. The problem is that Spring proxies the service anyway, even though it does not have any aop-requiring annotations.
c
With Configuration classes and bean methods you skip the proxies
w
the proxies are there for all the automagical building of the dependency graph with component scans. i prefer to avoid all that by using the functional bean declarations in spring boot 2.x (which can be extended even further such as in the spring-fu project, which create a very ktor-like API for spring boot)
👍 1
m
I’d rather not use Spring altogether in my project. I just did because I was used to, I think Ktor would be a better fit.
But I’ll check out spring-fu, thanks for the tip 🙂