簡體   English   中英

Spring 與構造函數 arguments 的自動裝配依賴關系

[英]Spring autowiring dependency with constructor arguments

我創建了一個服務 class,TestService,它的構造函數接受一個字符串數組,並有一點邏輯來返回一個長度大於 5 的字符串列表。

如何使用字符串參數數組將其注入到我的 Controller class 中?

@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;
    }
}

StringString[]不是 bean class,所以你不能使用構造函數Autowired來做到這一點。 但是您可以使用 Setter Autowired ,如下所示。

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();

        // Autowired using setter
        testService.setTestStrings(testStrings);
        // I want to run testService.getStringsOverLength5() on testStrings

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

@Service
class TestService {

    private String[] testStrings;

    public List<String> getStringsOverLength5() {
        return Arrays.stream(testStrings)
                .filter(s -> s.length() > 5)
                .collect(Collectors.toList());
    }
    
    @Autowired(required=false)
    public void setTestStrings(String[] testStrings) {
        this.testStrings = testStrings;
    }
}

我認為這是不可能的。 而是注入String[] testStrings持有者:

@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());
    }
}

但是,在此示例中,最好將testStrings作為getStringsOverLength5方法參數傳遞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM