What is the kotlin alternative for
@Autowired(required=false)
private DependencyC dependencyC;
and
private Optional<HelloService> optionalHelloService;
public HelloController(Optional<HelloService> helloService) {
this.optionalHelloService = helloService;
}
The accepted answer is outdated. Spring uses the type information to infer whether a bean is optional. See https://docs.spring.io/spring/docs/current/spring-framework-reference/languages.html#kotlin-annotations, specifically
In a similar fashion, Spring bean injection with @Autowired, @Bean, or @Inject uses this information to determine if a bean is required or not.
Because @Autowired is optional for constructor parameters, the shortest Kotlin alternative is
class HelloController(private val optionalHelloService: HelloService?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With