我的控制器:
@RestController
@RequestMapping("/mypath")
public class MyController {
@Autowired
MyServiceInterface service;
@PostMapping("/{source}")
void myControllerFunc(@PathVariable String source, @RequestBody MyObject obj) {
...
Object myServiceObj = service.myServiceFunc(param);
...
}
}
我的服务界面:
public interface MyServiceInterface {
Object myServiceFunc(String param);
}
我的服务障碍:
@Service
public class MyServiceOne {
Object myServiceFunc(String param) {
...
}
}
@Service
public class MyServiceTwo {
void myServiceFunc(String param) {
...
}
}
我的春季启动版本:1.5.7
我想根据路径变量(“源”)注入服务.如果source = 1,则注入MyServiceOne;如果source = 2,则注入MyServiceTwo.
这可能吗?
解决方法:
听起来您需要同时使用这两者,并且控制器上的每个方法调用都可以选择不同的方法.因此,用限定符区分这两种实现.在controller方法中使用path变量,并使其以编程方式决定要调用的服务.
标签:spring-boot,dependency-injection,java
来源: https://codeday.me/bug/20191108/2008505.html