使用 Spring Boot 设计类,使用泛型类型的回调设计 JPA

Design classes using Spring boot, JPA using callbacks of generic type

提问人:user2441441 提问时间:4/23/2023 最后编辑:user2441441 更新时间:4/24/2023 访问量:120

问:

当涉及回调时,我对使用 Spring boot 和 JPA 的设计感到困惑。

例:

Controller 将调用 Service 类并传入 T 的值,即在我们的示例中为 Employee:

@Controller
public class Controller1{
 
 @Autowired
 Service1 service1;

 @RequestMapping("/employee")
  public ResponseEntity<Object> employees(Request request){
    //Since employee is called I know that T should be of type Employee
    service1.runService(new Employee()); //I want to pass the type T as 
                                       //Employee, so that in the 
                                       //database all Employees can be saved
  }


}

我有一项服务:

//Controller will call this service and pass in T type
@Service
public class Service1{
  @Autowired
  Dependency1 dependency1;

 //@Autowire CallbackImpl -> this won't work because I need to create a new 
 //                          instance of type T for each different request from 
 //                            controller


public void runService(T t){
//CallbackImpl<Employee> callbackImpl= new CallbackImpl<>();-> this won't work because 
                                       //           it is not managed by Spring
                                       //           and the Repository bean 
                                       //           throws null pointer exception
  
   //Here I want to invoke the callbackImpl<T> class and specify the type T.. 
   // ..(which will be  an Entity type)
  
  ThirdPartyClass.get(callbackImpl) // callbackImpl is of type T (i.e Employee 
                                    // in this example)
 }
}

此 ThirdPartyClass.get() 方法提供接口 IThirdPartyCallback。

我的类 CallbackImpl 实现了这个接口,获取值并将其保存到数据库中,如下所示:

CallbackImpl<T> implements IThirdPartyCallback{
 
@Autowired
BaseRepository<T, Long> baseRepository // The sub-class EmployeeRepository 
                                       //..that 
                                      //..implements BaseRepository is called 
                                      //..based on type T

@Override
public void callback(String result){
// convert result to List<T> type, in this example T is Employee

baseRepository.saveAll(listOfResultsOfTypeT);  //in this result will be converted to List<T> 
                                 //and saved. In this example result will be 
                                 // List<Employee>
}
  }

我需要CallbackImpl的多个实例/bean。为了实现这一目标,推荐的 wat 是什么?

  1. 我可以创建一个带有@Configuration的工厂,并让一个方法为每个请求返回一个新的 CallbackImpl bean

  2. 也许我可以从 BaseRepository 中删除 Autowired,并通过 Service -> 传入 BaseRepository 这是一个很好的设计吗?即 Repository 应该只在 Service 中定义,并且 Service 在需要时使用辅助类传递 Repository bean?

  3. 将 CallbackImpl 声明为原型范围。我不清楚我将如何每次使用这种方法使用不同类型的 T 调用创建新实例。

  4. 也许我可以为要保存在数据库表中的 N 个实体创建具有 N 个服务的 N 个存储库。并且还有 N 个控制器,所以整个问题就消除了,但是如果我所做的只是保存不同类型的实体,如员工、承包商、主席等,即不相关的实体,这看起来像是重复了很多类似的代码。

  5. 以某种方式使用 EntityManger 来持久化不同的实体

或者这完全是疯狂的,并且有一种更好的方法来使用更好或更简单的设计来获得我想要的东西?

java 设计模式 spring-data-jpa 回调

评论

0赞 user2441441 4/24/2023
@xerx593用户:592355 stackoverflow.com/users/592355/xerx593 发生了什么?:/
1赞 xerx593 4/24/2023
对不起,我删除了,但希望您仍然拥有“有用的链接”......更深入,我开始怀疑我的答案/方法
1赞 xerx593 4/25/2023
在这里,我们可以玩:)
1赞 xerx593 4/25/2023
因此,首先,我们应该澄清/确定:我们需要“原型”范围吗?(因为这让一些人复杂化)......对于“通用豆”(定义),这就是汗水!
1赞 xerx593 4/25/2023
在测试中,我仅将其(带有相应通用参数的 ObjectProvider)用作所有服务的“集合”,在其他地方,我使用它来获取“新鲜原型”

答: 暂无答案