I can get it working using field autowired.
@Component
public class ZMyServiceConstructor {
@Autowired
private RestTemplate restTemplate;
@Value("${my_test_property}")
private String url;
}
I'd like to use constructor autowried. When I don't add any construtor, or I use either @NoArgsConstructor or @RequiredArgsConstructor, url has value. But restTemplate is null
@Component
//@NoArgsConstructor
//@RequiredArgsConstructor
public class ZMyServiceConstructor {
private RestTemplate restTemplate;
@Value("${my_test_property}")
private String url;
}
If I use @AllArgsConstructor, it gives me error:
required a bean of type 'java.lang.String' that could not be found
How to get this working?
Updated:
Adding final to field restTemplate for lombok to build a constructor
@Value can't be used in final field. The field will be injected after constructor is called. @Value annotation not working in constructor
@Component @RequiredArgsConstructor public class ZMyServiceConstructor { private final RestTemplate restTemplate; @Value("${my_test_property}") private String url; }
This equals to below
@Component
public class ZMyServiceConstructor {
private final RestTemplate restTemplate;
@Value("${my_test_property}")
private String url;
@Autowired
public ZMyServiceConstructor(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
}