有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带有构造函数参数的java Spring自动关联依赖项

我创建了一个服务类TestService,它的构造函数接受一个字符串数组,并有一个小逻辑来返回长度大于5的字符串列表

如何使用字符串数组参数将其注入控制器类

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

共 (2) 个答案

  1. # 1 楼答案

    StringString[]不是bean类,因此不能使用构造函数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;
        }
    }
    
  2. # 2 楼答案

    我认为这是不可能的。而是注入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方法参数传递