spring框架中根据实体对象调用不同的Service api

发布于 2024-10-06 10:38:37 字数 438 浏览 0 评论 0原文

我正在开发一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2024-10-13 10:38:37

但在特定情况下,我有活动对象(不知道它是哪个子类)并且必须编写一个对于所有实体对象都不同的方法。

只需将基类抽象化并定义每个子类必须实现的抽象方法即可:

public abstract class ActivityService{
    public abstract Foo processEntity(Entity entity);
}

问题:有没有办法,我可以根据我拥有的实体对象调用不同的服务。(我拥有的对象是实体,而不是服务,我无法对其进行任何硬编码)

这​​是您应该尽量避免的情况。通常,您应该只将实体发送到知道如何处理它的服务,而不是发送到一堆服务,其中一个服务负责。但我要做的是使用调度程序服务来保存服务负责的类的映射。它使用这样的逻辑:

private Map<Class<? extends Entity>,
    Class<? extends ActivityService>> serviceMap =
        new ConcurrentHashMap<Class<? extends Entity>,
                              Class<? extends ActivityService>>();
private ApplicationContext context;
private ActivityService getServiceForEntity(Entity e){
    Class<? extends Entity> entityClass = e.getClass();
    Class<? extends ActivityService> serviceClass =
        serviceMap.get(entityClass);
    if(serviceClass==null){
        for(Map.Entry<Class<? extends Entity>,
                      Class<? extends ActivityService>> entry :
            serviceMap.entrySet()){
            if(entry.getKey().isAssignableFrom(entityClass)){
                serviceClass = entry.getValue();
                break;
            }
        }
        if(serviceClass==null){
            // throw a suitable exception
        }
        serviceMap.put(entityClass, serviceClass);
    }
    return context.getBean(serviceClass);
}

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.

Just make the base class abstract and define an abstract method that each sub class has to implement:

public abstract class ActivityService{
    public abstract Foo processEntity(Entity entity);
}

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

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:

private Map<Class<? extends Entity>,
    Class<? extends ActivityService>> serviceMap =
        new ConcurrentHashMap<Class<? extends Entity>,
                              Class<? extends ActivityService>>();
private ApplicationContext context;
private ActivityService getServiceForEntity(Entity e){
    Class<? extends Entity> entityClass = e.getClass();
    Class<? extends ActivityService> serviceClass =
        serviceMap.get(entityClass);
    if(serviceClass==null){
        for(Map.Entry<Class<? extends Entity>,
                      Class<? extends ActivityService>> entry :
            serviceMap.entrySet()){
            if(entry.getKey().isAssignableFrom(entityClass)){
                serviceClass = entry.getValue();
                break;
            }
        }
        if(serviceClass==null){
            // throw a suitable exception
        }
        serviceMap.put(entityClass, serviceClass);
    }
    return context.getBean(serviceClass);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文