spring框架中根据实体对象调用不同的Service api
我正在开发一个使用 Spring 框架设计的 Web 服务应用程序,其中我有不同的实体类(ActivityA、ActivityB、ActivityC...),它们继承自基类“activity”
现在我已经在服务层中编写了不同的服务 api基类和所有子类。 (命名为ActivityService,ActivityAService,ActivityBService,ActivityCService..)
所有对每个活动操作类似的方法都放在基类(ActivityService)的服务api中,并放在各自的服务中。
我通常知道我正在处理哪个对象,并调用相应的服务 api。但在特定情况下,我有活动对象(不知道它是哪个子类)并且必须编写一个对于所有实体对象都不同的方法。
问题:有没有办法,我可以根据我拥有的实体对象调用不同的服务。(我拥有的对象是实体,而不是服务,我无法进行任何硬编码来获取服务对象)
I am working on a web service application designed using spring framework, where I have different entity classes(ActivityA,ActivityB,ActivityC...) which inherit from a base class "activity"
Now I have written different service api's in service layer for the base class and all the child classes. (to name, ActivityService,ActivityAService,ActivityBService,ActivityCService..)
All the methods which operate similar for each activity are put in service api of base class(ActivityService) and rest in their respective services.
I generally know which object I am working on, and I call the respective service api. But in a particular case I have the activity object(don't know of which child class it is) and have to write a method which is different for all entity objects.
PROBLEM : Is there a way,I can call different service's based on the entity object I have.(the object I have is of entity, not service and I cant do any hard coding to get Service object)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将基类抽象化并定义每个子类必须实现的抽象方法即可:
这是您应该尽量避免的情况。通常,您应该只将实体发送到知道如何处理它的服务,而不是发送到一堆服务,其中一个服务负责。但我要做的是使用调度程序服务来保存服务负责的类的映射。它使用这样的逻辑:
Just make the base class abstract and define an abstract method that each sub class has to implement:
This is a situation you should try to avoid. Usually, you should only send an entity to a service that knows what to do with it, and not to a bunch of services, one of which is in charge. But what I'd do is use a dispatcher service that keeps a map of classes that the services are in charge of. It uses logic like this: