简体   繁体   中英

Spring Boot dependency injecting with request scoped beans

I am trying to dependency inject one bean into another, but I would like to do it by simply Autowiring directly into the class, instead of via the "@Configuration class". Here is the controller class:

@Controller
public class RestfulSourceController {

    @Autowired
    Response response;

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

}

And here are to "@Configuration" classes with a bean declared inside each

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

}

and

@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*/);
    }

}

And finally here is the Response class:

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)
    }
}

However, when I set up the dependency injection like this, I get a null pointer exception because the autowired bean "requestServiceConfig" is not instantiated. How can I dependency inject the bean with autowiring so that I don't have to pass the reference to the bean via the constructor?

Problem is the use of the requestServiceConfig in the constructor of Response . Autowiring can only happen after object creation so the dependency can only be null here.

To solve the problem and be able to use autowiring, you could move the code from the constructor to the @PostConstruct lifecycle method:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM