Say I have a class like this: ```@RestController c...
# spring
h
Say I have a class like this:
Copy code
@RestController
class Foo(val bar: Bar, val baz: Baz = Baz("ding"))
bar
and
baz
will be autowired automatically as expected. But is it possible to disable autowiring for
baz
since it has a default value?
t
Just out of interest, what is the usecase? In case spring does not have a Baz bean, use this default one instead? (within spring that would be solved with conditional bean loading)
and if you want it to be a property of the class but not being set by the constructor you can just create a separate property https://kotlinlang.org/docs/properties.html
h
it's convenient to have all dependencies in the constructor when testing Foo
And Baz is maybe not available for autowiring now, but someone might add it later, thus braking Foo (or introducing interesting side effects). This exact thing happened in a project here earlier.
t
So it is a non-bean dependency, and you want to change it during testing? (if that is the case I think I would consider making it a bean)
Or you can give it as an argument to functions who actually need to use Baz
h
So it is a non-bean dependency, and you want to change it during testing
Yup. I was trying to avoid making it a bean, but it seems to be the only viable option. I was hoping for a
@DontAutowire
annotation or something 🙂
j
Sounds like you might be able to achieve this with a testing only constructor and di constructor
Ie the di constructor delegates to the testing constructor and passes in the default arg