Java:不同类实例的方法实现

时间:2016-10-07 07:56:35

标签: java spring spring-mvc inheritance abstract-class

使用Spring Boot进行Java项目。

我有一个通用服务

@Service
public class GenericService 

继承GenericService的几个类:

@Service
public class Entity1Service extends GenericService

@Service
public class Entity2Service extends GenericService

实体类:

@MappedSuperclass
public class AbstractEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ENTITY_SEQ")
    private Long id;

    @Enumerated(EnumType.STRING)
    private ProcessState processState;
}

@Entity
public class Entity1 extends AbstractEntity {
     // some specific fields
}

@Entity
public class Entity2 extends AbstractEntity {
     // some specific fields
}

我必须在子类中编写类似的方法,在基类中编写一个泛型:

@Service
public class Entity1Service extends GenericService {
     public void startEntity1Process(AbstractEntity entity) {
       Entity1 entity1 = (Entity1) entity; 
       // perform some specific operations 
    }
}

@Service
public class Entity2Service extends GenericService {
     public void startEntity2Process(AbstractEntity entity) { 
        Entity2 entity2 = (Entity2) entity; 
       // perform some specific operations 
    }
}

我在基类中编写泛型方法:

@Service
public class GenericService {

@Autowired
Entity1Service entity1Service;

@Autowired
Entity1Service entity2Service;

     public void startEntityProcess(AbstractEntity entity) { 
        if (entity instance of Entity1)
            entity1Service.startEntity1Process(entity);
        else if (entity instance of Entity2)
            entity2Service.startEntity2Process(entity);
    }
}

我收到了错误:

  

由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型[com.process.Entity1Service]的限定bean用于依赖:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}       at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)~ [spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]

我建议,错误的原因是我正在尝试在父类中自动装配子类的bean。

1。如何在当前实施中避免此错误?

2。有没有更好的解决方案来解决这类任务?

也许我要尝试将public void startEntityProcess(AbstractEntity entity)声明为抽象,然后再将其删除?但是我将如何区分实体的实例呢?

2 个答案:

答案 0 :(得分:1)

我会推荐一种不同的方法。

更改此

@Service
public class GenericService 

public interface GenericService {
  void startEntityProcess(AbstractEntity entity);
}

并进行两次实施

@Service
public class Entity1Service implements GenericService {
     public void startEntityProcess(AbstractEntity entity) {
       Entity1 entity1 = (Entity1) entity; 
       // perform some specific operations 
    }
}

@Service
public class Entity2Service implements GenericService {
     public void startEntityProcess(AbstractEntity entity) { 
        Entity2 entity2 = (Entity2) entity; 
       // perform some specific operations 
    }
}

然后根据需要注入Entity1Service或Entity2Service

或另一种选择就是

public interface EntityService {
  void startEntityProcess(AbstractEntity entity);
}

@Service
public class EntityServiceImpl implements EntityService {
     public void startEntityProcess(AbstractEntity entity) { 
 // if instance of Entity 1 call startEntity1Process
 // if instance of Entity 2 call startEntity2Process
    }

public void startEntity1Process(Entity1 entity) {}
public void startEntity2Process(Entity2 entity) {}
}

答案 1 :(得分:1)

可以使用@Autowired和构造函数来解决。

将GenericService更改为

@Service
public class GenericService {

    Entity1Service entity1Service;

    Entity2Service entity2Service;

    @Autowired
    public GenericService(Entity1Service entity1Service, Entity2Service entity2Service){
        this.entity1Service=entity1Service;
        this.entity2Service=entity2Service;
    }

    public GenericService(){

    }

     public void startEntityProcess(AbstractEntity entity) { 
        if (entity instance of Entity1)
            entity1Service.startEntity1Process(entity);
        else if (entity instance of Entity2)
            entity2Service.startEntity2Process(entity);
    }
}

或只使用@Autowired(required = false)

@Service
public class GenericService {
    @Autowired(required=false)
    Entity1Service entity1Service;

    @Autowired(required=false)
    Entity2Service entity2Service;

     public void startEntityProcess(AbstractEntity entity) { 
        if (entity instance of Entity1)
            entity1Service.startEntity1Process(entity);
        else if (entity instance of Entity2)
            entity2Service.startEntity2Process(entity);
    }
}

我测试了它。它可以使用其中任何一个。