EJB,无法在 Json 响应方法中注入
EJB, can't get injection in Json response method
我的 EJB 有问题:
public class EntryResponseBuilder extends BaseResponseBuilder {
@EJB
private ApplicationCustomerCache applicationCustomerCache; //null
我在 class 中有类似的内容 returns JSON 响应。
@Singleton
@Startup
public class ApplicationCustomerCache {
@EJB
CustomerDao customerDao;
private Map<Integer, Customer> customerMap;
private Map<Integer, List<Customer>> applicationToCustomersMap;
private static Logger log = Logger.getLogger(ApplicationCustomerCache.class);
@PostConstruct
public void initialize() {
try {
get();
} catch (Exception e) {
log.error("ApplicationCustomerCache.init()", e);
}
}
private void get() throws SQLException {
它工作正常,除了当我尝试在 EntryResponseBuilder
中进行注射时,它是空的。之前有一个函数,它注入了 class 和 @Stateless
。我在 Google 中搜索过,但找不到任何解决方案。
您在这里处理两个问题:
- 您正在尝试在 POJO 上注入一个 bean 实例,该实例不受容器管理;
- 您遵循的 EJB 规范是什么?
对于这两种情况,我认为以下答案可以帮助到您:
我用InitialContext()
解决了
ApplicationCustomerCache applicationCustomerCache = (ApplicationCustomerCache) new InitialContext().lookup("java:comp/ApplicationCustomerCache");
我的 EJB 有问题:
public class EntryResponseBuilder extends BaseResponseBuilder {
@EJB
private ApplicationCustomerCache applicationCustomerCache; //null
我在 class 中有类似的内容 returns JSON 响应。
@Singleton
@Startup
public class ApplicationCustomerCache {
@EJB
CustomerDao customerDao;
private Map<Integer, Customer> customerMap;
private Map<Integer, List<Customer>> applicationToCustomersMap;
private static Logger log = Logger.getLogger(ApplicationCustomerCache.class);
@PostConstruct
public void initialize() {
try {
get();
} catch (Exception e) {
log.error("ApplicationCustomerCache.init()", e);
}
}
private void get() throws SQLException {
它工作正常,除了当我尝试在 EntryResponseBuilder
中进行注射时,它是空的。之前有一个函数,它注入了 class 和 @Stateless
。我在 Google 中搜索过,但找不到任何解决方案。
您在这里处理两个问题:
- 您正在尝试在 POJO 上注入一个 bean 实例,该实例不受容器管理;
- 您遵循的 EJB 规范是什么?
对于这两种情况,我认为以下答案可以帮助到您:
我用InitialContext()
ApplicationCustomerCache applicationCustomerCache = (ApplicationCustomerCache) new InitialContext().lookup("java:comp/ApplicationCustomerCache");