yN.
yN.

Reputation: 2267

Spring Constructor with 2 arguments where one need to be injected

I would like to create an instance of a RestTemplate inside the constructor of a class using the RestTemplateBuilder. I would like to autowire the RestTemplateBuilder in the constructor and pass another parameter "manually". Is this possible?

My approach so far:

public class IlikeU {

    private final String blub;
    private final RestTemplate restTemplate;

        public IlikeU(final String blub, @Autowired final RestTemplateBuilder restTemplateBuilder) {
            this.blub = blub;
            this.restTemplate = restTemplateBuilder.build();
        }
    }

I want to create an instance like the following but cant because the constructor expects 2 params.

IlikeU iLikeU = new IlikeU("pew pew");

Upvotes: 1

Views: 1735

Answers (3)

DEVAS
DEVAS

Reputation: 354

Additional to FranXho's answer you can also use @Required With autowired if you don't want to loose advantage of constructor dependencey.

Upvotes: 0

FranXho
FranXho

Reputation: 765

You can't do exactly like that. How about alternative way, like in following code?

@Component
public class IUseU{
    @Autowired
    private RestTemplateBuilder restTemplateBuilder;

    public void use(){
        IlikeU iLikeU = new IlikeU("pew pew", restTemplateBuilder);
    }
}

public class IlikeU {

    private final String blub;
    private final RestTemplate restTemplate;

    public IlikeU(final String blub, final RestTemplateBuilder restTemplateBuilder) {
        this.blub = blub;
        this.restTemplate = restTemplateBuilder.build();
    }
}

Side Note: The advantage of spring framework is dependency injection. So if you are using spring, you should not use new XXXX() in your code and let spring framework handle that part for you.

By doing that, you get advantage of loose coupling. You can replace that class with different one later. Otherwise, over long run, you will find yourself very difficult to replace any class.

Upvotes: 1

Rafał Wrzeszcz
Rafał Wrzeszcz

Reputation: 2067

It doesn't work like that - you either pass object creation to Spring, or handle it yourself. Spring is not a pre-processor that will expose you a different interface - it will use your interface to handle it automatically.

What you can do, is to also provide bluc value to the Spring context and automate it completely:

public IlikeU(@Value("${your.parameter}") final String blub, @Autowired final RestTemplateBuilder restTemplateBuilder)

You need to define your value as a key your.parameter in the properties available to your context.

Other possibility is to build a factory, that will get a rest template builder from Spring and build your IlikeU class with given string:

public class IlikeUFactory {
    private final RestTemplate restTemplate;

    public IlikeUFactory(@Autowired final RestTemplateBuilder restTemplateBuilder) {
        this.restTemplate = restTemplateBuilder.build();
    }

    public IlikeU createIlikeU(String blub) {
        return new IlikeU(blub, this.restTemplate);
    }
}

Upvotes: 2

Related Questions