English 中文(简体)
不同呼吁 基于实体在春季框架中的标的的服务单
原标题:Call different Service api s based on the entity object in spring framework

我正在研究一个利用春季框架设计的网络服务应用程序,在那里,我有不同的实体类别(活动A、活动B、活动C......)继承基类“活动”。

现在,我写了基础班级和所有儿童班级的不同服务站。 (姓名、活动服务、活动服务、活动服务、活动服务)

每项活动采用的所有类似方法都用于基类(活动服务)的复印件,并用于各自的服务。

我一般知道我正在讨论哪些目标,我称之为各自的服务站。 但在特定情况下,我有活动反对(不知道哪类儿童)并且必须写出一种对所有实体物体都不同的方法。

PROBLEM: 如果有办法,我可以称之为基于实体目标一的不同服务。

最佳回答

但在特定情况下,我有活动反对(不知道哪类儿童)并且必须写出一种对所有实体物体都不同的方法。

简单地使基类抽象,界定每个子类别必须执行的抽象方法:

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

PROBLEM: 如果有办法,我可以称之为基于实体目标一的不同服务。

这是你应努力避免的情况。 通常,你只应派遣一个实体来提供服务,了解与它做些什么,而不是一个负责的部门。 但是,我这样做的是使用一个发送器服务,保存一个服务所负责的班级地图。 它采用这样的逻辑:

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);
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签