@RequestBody用于确定要自动装配的bean

时间:2018-02-27 17:26:24

标签: java spring

根据进入控制器的@RequestBody有效负载(或POST URL),有没有办法可以调节bean的创建?

我有接口B和类ServiceB。 ServiceB依赖于B.我已在应用程序类

中对其进行了配置
@SpringBootApplication
public class Application {

public static void main(String[] args) {
    SpringApplication.run(ServiceNowMediatorApplication.class, args);
}
@Bean
public B b(){
    return new BImpl();
}


@Bean
public ServiceB serviceB(B b){
    return new ServiceB(b);
}

}

BImpl类实现接口B

接收b对象作为POST调用的主体

@RestController
@RequestMapping("/{database}/alerts")
public class ControllerB {
  @Autowired private ServiceB serviceB;

  @Autowired
  private B b;

  @Autowired
  ControllerB(B b,ServiceB serviceB){
      this.b = b;
      this.serviceB = serviceB;
  }

  @RequestMapping(method = RequestMethod.POST)
  public B dosomethingCrazy(@RequestBody BImpl bimpl)  {


      String response = serviceB.dosomethingImportant();
      return bimpl;

  }

}

ServiceB类

@Service
public class ServiceB {


@Autowired
public B b;

@Autowired
public B getB() {
    return b;
}

@Autowired
public void setB(B b) {
    this.b = b;
}

@Autowired
public ServiceB(B b){
    this.b = b;

}

如果我有一个也实现B的ServiceC类,我如何根据RequestBody在控制器中自动装配?

非常感谢!

0 个答案:

没有答案