• To create a new object in spring without using the new keyword,using @Configuration & @Bean
public class AppConfig{
    RestTemplate restTemplate(){
        return new RestTemplate();

    Accounts accounts(){
        return new Accounts();

Now autowire RestTemplate or Accounts in the service layer, this is the standard way of creating new objects in spring using bean definitions

  • Rest Template
RestTemplate restTemplate;

public AccountResponseDto addNewAccountCustomer(AccountRequestDto accountDto){
    String url="http://localhost:8081/api/v1/customer/{id}"
    ResponseEntity<CustomerDto> response=restTemplate.getForEntity(url,CustomerDto.class,accountDto.customerId());