Spring autowiring dependency with constructor arguments



I have created a service class, TestService whose constructor takes an array of Strings and has a little logic to return a list of Strings with length greater than 5.

How do I get about injecting this into my Controller class with the array of Strings argument?

@RestController
public class Controller {

    private final TestService testService;

    @Autowired
    public Controller(TestService testService) {
        this.testService = testService;
    }

    @PostMapping("test")
    public List<String> test(@RequestBody TestDTO request) {
        String[] testStrings = request.getTestStrings();

        // I want to run testService.getStringsOverLength5() on testStrings

        List<String> strings = testService.getStringsOverLength5();
        return strings;
    }
}

@Service
public class TestService {

    private final String[] testStrings;

    public TestService(String[] testStrings) {
        this.testStrings = testStrings;
    }

    public List<String> getStringsOverLength5() {
        return Arrays.stream(testStrings)
                .filter(s -> s.length() > 5)
                .collect(Collectors.toList());
    }
}

public class TestDTO {

    private String[] testStrings;

    public TestDTO() {}

    public String[] getTestStrings() {
        return testStrings;
    }

    public void setTestStrings(String[] testStrings) {
        this.testStrings = testStrings;
    }
}

Answer

I think it is impossible. Instead inject String[] testStrings holder:

@Component
@Scope(scopeName = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class TestStringsHolder {

    private String[] testStrings;

    public String[] getTestStrings() {
        return testStrings;
    }

    public void setTestStrings(String[] testStrings) {
        this.testStrings = testStrings;
    }
}
@RestController
public class Controller {

    private final TestService testService;
    private final TestStringsHolder holder;

    @Autowired
    public Controller(TestService testService, TestStringsHolder holder) {
        this.testService = testService;
        this.holder = holder;
    }

    @PostMapping("test")
    public List<String> test(@RequestBody TestDTO request) {
        String[] testStrings = request.getTestStrings();
        holder.setTestStrings(testStrings);

        List<String> strings = testService.getStringsOverLength5();
        return strings;
    }
}
@Service
public class TestService {

    private final TestStringsHolder holder;

    public TestService(TestStringsHolder holder) {
        this.holder = holder;
    }

    public List<String> getStringsOverLength5() {
        final String[] testStrings = holder.getTestStrings();

        return Arrays.stream(testStrings)
            .filter(s -> s.length() > 5)
            .collect(Collectors.toList());
    }
}

However, in this example, it seems better to just pass testStrings as getStringsOverLength5 method argument.



Source: stackoverflow