java - Spring Boot 使用请求范围的 bean 进行依赖注入(inject)

标签 java spring web-services spring-mvc

我正在尝试将一个bean依赖注入(inject)到另一个bean中,但我想通过简单地直接 Autowiring 到类中来实现这一点,而不是通过“@Configuration类”。这是 Controller 类:

@Controller
public class RestfulSourceController {

    @Autowired
    Response response;

    @RequestMapping(value="/rest", method=RequestMethod.GET, produces="application/json")
    @ResponseBody
    public Object greeting() {
        return response.getResponse();
    }

}

这里是“@Configuration”类,每个类中都声明了一个 bean

@Configuration
class RequestConfigurationBeans {

    @Autowired
    private ServicesRepository servicesRepo;

    @Autowired
    private HttpServletRequest request;

    @Bean(name = 'requestServiceConfig')
    @Scope(value=WebApplicationContext.SCOPE_REQUEST, proxyMode=ScopedProxyMode.TARGET_CLASS)
    public ServiceModel requestServiceConfig(){
        String serviceName = RequestUtil.getServiceName(this.request)
        ServiceModel serviceModel = servicesRepo.findByName(serviceName)
        return serviceModel
    }

}

@Configuration
public class ServletFilterBeans {

    /* I don't want to autowire here, instead I want to autowire in the Response class directly, instead of passing the bean reference into the constructor
    @Autowired
    ServiceModel requestServiceConfig
    */

    @Bean
    @Scope(value=WebApplicationContext.SCOPE_REQUEST, proxyMode=ScopedProxyMode.TARGET_CLASS)
    public Response response(){
        return new Response(/*requestServiceConfig*/);
    }

}

最后是 Response 类:

class Response {

    //I want to Autowire the bean instead of passing the reference to the constructor
    @Autowired
    ServiceModel requestServiceConfig

    Object response

    public Response(/*ServiceModel requestServiceConfig*/){
        //this.requestServiceConfig = requestServiceConfig

        if (requestServiceConfig.getType() == 'rest'){
            this.response = getRestfulSource()
        }

    }


    private Object getRestfulSource(){
        RestTemplate restTemplate = new RestTemplate()
        String url = requestServiceConfig.sourceInfo.get('url')
        return restTemplate.getForObject(url, Object.class)
    }
}

但是,当我像这样设置依赖注入(inject)时,我收到一个空指针异常,因为 Autowiring 的 bean“requestServiceConfig”未实例化。如何使用 Autowiring 来依赖注入(inject) bean,这样我就不必通过构造函数传递对 bean 的引用?

最佳答案

问题是在 Response 的构造函数中使用了 requestServiceConfig。 Autowiring 只能在对象创建后发生,因此这里的依赖关系只能为空。

要解决问题并能够使用 Autowiring ,您可以将代码从构造函数移动到 @PostConstruct 生命周期方法:

@PostConstruct
private void init() {
    if ("rest".equals(this.requestServiceConfig.getType())) {
        this.response = getRestfulSource();
    }
}

关于java - Spring Boot 使用请求范围的 bean 进行依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41069214/

相关文章:

spring - 如何使用 Reactor (Spring WebClient) 做重复调用?

spring - Spring事务中发生死锁时调度程序停止

java - 抽象属性是否违反 Liskov 替换原则?

java - 如何在java中验证Long数据类型字段

java.lang.ClassNotFoundException : org. apache.commons.dbcp.BasicDataSource 错误

java - 如何为 MERGE 语句发送适当的 HTTP 状态?

javascript - 无法让 jQuery 和 Web 服务很好地发挥作用

java - 在另一个 toast 替换它之前,如何确保 toast 在屏幕上显示特定时间?

Java - 我正在编写我的第一个项目。这是二十一点,但我遇到了一些麻烦

web-services - 是否有任何在线 API,可以提供不同国家/地区的免费电话和紧急电话号码的更新列表