gpt4 book ai didi

java - EJB 无法解析引用,因为应用程序中存在多个具有相同接口(interface)的 ejb

转载 作者:行者123 更新时间:2023-12-01 09:09:32 24 4
gpt4 key购买 nike

免责声明:我对 EJB 的整个概念相当陌生

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

(对错误的代码格式表示歉意)

我尝试注入(inject)的类是:

@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);
}
}

他们的共同接口(interface)是:

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.为您的 EJB 命名:

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

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

并告诉客户端要注入(inject)哪个 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;

关于java - EJB 无法解析引用,因为应用程序中存在多个具有相同接口(interface)的 ejb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41007692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com