简体   繁体   English

具有运行时参数的Spring IOC DI

[英]Spring IOC DI with runtime parameters

I'm relativity new to IOC and DI, so I'm guessing that I am missing some high-level design principle here, but I cannot figure out how to get my architecture working. 我是IOC和DI的相对新手,所以我猜想我在这里缺少一些高级设计原理,但是我不知道如何使我的体系结构正常工作。

I have a REST API endpoint that accepts two pieces of POST data: customer ID, and Type ID. 我有一个REST API端点,它接受两段POST数据:客户ID和类型ID。 The rest api then needs to return a set of data for that specific customer/type combo. 然后,其余api需要针对该特定客户/类型组合返回一组数据。

Here is a crude picture of what I am doing: 这是我在做什么的粗略图片: 在此处输入图片说明

The controller is taking the entity IDs passed in via post data, and via a JPA repository getting the appropriate Entities for them. 控制器将获取通过发布数据传递的实体ID,并通过JPA信息库获取适当的实体。

I then construct a data generator object (that takes the entities as constructor parameters), and use that to handle all of the data gathering for the API. 然后,我构造一个数据生成器对象(将实体作为构造函数参数),并使用该对象来处理API的所有数据收集。

The Problem: because the Data Generator takes the two dynamic constructor parameters, it cannot be DI'ed into the Controller, but instead must be made with new . 问题:由于数据生成器采用了两个动​​态的构造函数参数,因此无法将其直接添加到Controller中,而必须使用new来进行构造。 Inside of the Data Generator, however, I need access to JPA repositories. 但是,在数据生成器内部,我需要访问JPA存储库。 The only way to get access to these repositories is via DI. 访问这些存储库的唯一方法是通过DI。 I cannot DI however, as the object was new 'ed not DI'ed by the IOC container. 但是,我无法进行DI,因为该对象是IOC容器的new “ ed not DI”。

Is there a way to architect this so that I don't have this problem? 有没有一种架构方法,这样我就不会遇到这个问题? Am I breaking some rule regarding IOC? 我是否违反了有关IOC的规定? Do I have wrong assumptions somewhere? 我在某处是否有错误的假设? Any advice is appreciated. 任何建议表示赞赏。

Thanks! 谢谢!

Edit: Pseudo code for Data Generator 编辑:数据生成器的伪代码

public class DataGenerator {
    private Customer customer;
    private Type type

    public DataGenerator(Customer customer, Type type) {
        this.cusomter = customer;
        this.type = type;
    }

    public generateData() {
        if(customer == x && type == y) {
            //JPA REPOSITORY QUERY
        } else {
            //DIFFERENT JPA REPOSITORY QUERY
        }
    }
}

I think you may have gotten confused somewhere along the line. 我认为您可能已经迷茫了。 You should have a Service that hits your repositories, and provide the information to the controller. 您应该有一个可以访问您的存储库的Service ,并将信息提供给控制器。 One crude setup would be something like this. 一个粗略的设置就是这样。

@Controller 
public MyController {

    @AutoWired
    private DataService dataService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    private DataGenerator readBookmark(@PathVariable Long customerId, @PathVariable Integer typeId) {
        return dataService.getData(customerId, typeId);
    }
}

@Service
public class DataService {

    @AutoWired
    private JPARepository repository;

    public DataGenerator getData(long customerId, int typeId) {
        Type typeDetails = repository.getType(typeId);
        Customer customerDetails = repository.getCustomer(customerId);
        return new DataGenerator(customerDetails, typeDetails);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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