Spring 带有构造函数参数的自动装配依赖
Spring autowiring dependency with constructor arguments
我创建了一个服务 class,TestService,其构造函数接受一个字符串数组,并且对 return 长度大于 5 的字符串列表有一点逻辑。
如何使用字符串参数数组将其注入我的控制器 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;
}
}
String
或 String[]
不是一个 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
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());
}
}
但是,在此示例中,将 testStrings
作为 getStringsOverLength5
方法参数传递似乎更好。
我创建了一个服务 class,TestService,其构造函数接受一个字符串数组,并且对 return 长度大于 5 的字符串列表有一点逻辑。
如何使用字符串参数数组将其注入我的控制器 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;
}
}
String
或 String[]
不是一个 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
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());
}
}
但是,在此示例中,将 testStrings
作为 getStringsOverLength5
方法参数传递似乎更好。