EJB无法解析引用,因为应用程序中有多个ejbs具有相同的接口

时间:2016-12-07 01:02:50

标签: java java-ee dependency-injection ejb javabeans

免责声明:我对EJB

的整个概念都很陌生

我正在尝试创建一个使用EJB依赖注入机制的简单企业应用程序。我试图注入的对象是不同的(非泛型)类,它们都实现相同的(通用)接口,并且也作为该接口的类型暴露给它们的包含类。问题是EJB不知道在其包含的类中实例化所述对象所使用的类。我该如何解决这个问题?

(对于错误的代码格式化道歉)

我试图注入的课程是:

@Stateless
public class HumidityRepository implements ITimeStampedDataRepository<Humidity>, Serializable {

@PersistenceContext(unitName = "WeatherPU")
private EntityManager em;

@Override
public List<Humidity> getAll() {
    Query q = em.createQuery("allHumidity");
    System.out.println(q.getResultList());
    List<Humidity> result = q.getResultList();
    return result;
}

@Override
public List<Humidity> getAllOnDate(LocalDate date) {
    Query q = em.createQuery("onDateHumidity")
            .setParameter("date", date);
    System.out.println(q.getResultList());
    List<Humidity> result = q.getResultList();
    System.out.println(result);
    return result;
}

@Override
public List<Humidity> getAllAfter(LocalDate date, LocalTime time) {
    Query q = em.createQuery("afterHumidity")
            .setParameter("date", date)
            .setParameter("time", time);
    List<Humidity> result = q.getResultList();
    return result;
}

@Override
public void remove(LocalDate date, LocalTime time) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void store(Humidity data) {
    em.persist(data);
}

}

@Stateless
public class TemperatureRepository implements ITimeStampedDataRepository<Temperature>, Serializable {

@PersistenceContext(unitName = "WeatherPU")
private EntityManager em;

@Override
public List<Temperature> getAll() {
    Query q = em.createQuery("allTemperature");
    System.out.println(q.getResultList());
    List<Temperature> result = q.getResultList();
    return result;
}

@Override
public List<Temperature> getAllOnDate(LocalDate date) {
    Query q = em.createQuery("onDateTemperature")
            .setParameter("date", date);
    System.out.println(q.getResultList());
    List<Temperature> result = q.getResultList();
    System.out.println(result);
    return result;
}

@Override
public List<Temperature> getAllAfter(LocalDate date, LocalTime time) {
    Query q = em.createQuery("afterTemperature")
            .setParameter("date", date)
            .setParameter("time", time);
    List<Temperature> result = q.getResultList();
    return result;
}

@Override
public void remove(LocalDate date, LocalTime time) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void store(Temperature data) {
    em.persist(data);
}
}

他们的共同界面是:

public interface ITimeStampedDataRepository<T extends TimeStampedData> {

    public List<T> getAll();

    public List<T> getAllOnDate(LocalDate date);

    public List<T> getAllAfter(LocalDate date, LocalTime time);

    public void remove(LocalDate date, LocalTime time);

    public void store(T data);

}

最后,他们的包含类是:

@Dependent
public class Service implements IService {

    @EJB
    private ITimeStampedDataRepository<Humidity> humidityRepository;
    @EJB
    private ITimeStampedDataRepository<Temperature> temperatureRepository;

    ...

}

1 个答案:

答案 0 :(得分:1)

您有两种选择:

1.给你的EJB一个名字:

@Stateless(name="BeanNameOne")
public class TemperatureRepository implements.....

@Stateless(name="BeanNameTwo")
public class HumidityRepository implements.....

并告诉客户端注入哪个EJB。

@EJB(beanName="BeanNameOne")
private ITimeStampedDataRepository<Temperature> temperatureRepository;
@EJB(beanName="BeanNameTwo")
private ITimeStampedDataRepository<Humidity> humidityRepository;

2.请参阅不合格的名称

@EJB(beanName="TemperatureRepository ")
private ITimeStampedDataRepository<Temperature> temperatureRepository;
@EJB(beanName="HumidityRepository")
private ITimeStampedDataRepository<Humidity> humidityRepository;