Is there anyway in spring + kotlin do to a mixed constructor where you have e.g.
Copy code
@Service
class MyClass(
myService: MyService, // autowired
helper: Helper = Helper("defaultValue") // just get default value but somebody could override this in e.g. tests
)
?
n
Nick Howes
09/25/2021, 9:54 AM
Usually when we want to autowire from the context but override some beans we define a @Configuration as an inner class of the test class to override beans with test versions, which is supported by Spring TestContext. If it's Spring Boot there is also @TestConfiguration which is an alternative way to do it.
This is all standard Spring, nothing Kotlin specific.
t
Tobias Berger
09/27/2021, 11:21 AM
alternatively, you could also just create a secondary constructor with just one parameter and the @Autowired annotation
k
Klitos Kyriacou
10/05/2021, 2:32 PM
When I try this (as in the OP's example code), it works. IntelliJ gives an error saying the default argument can't be autowired, but if I just ignore the error, nothing bad seems to happen. The application runs as expected, and the unit tests run fine. It probably isn't recommended though.