@PostConstructでメソッドを呼び出す場合、メソッドは呼び出されません。私はサーバーからエラーやログを取得しません。いくつかの構成XMLファイルを追加するか、メソッドを呼び出すために注釈を追加する必要がありますか?EJB @PostConstructが呼び出されていない
Serlvet:
public class PersonServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger LOG = LoggerFactory.getLogger(PersonServlet.class);
@EJB
Storage storage;
protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
try {
HttpSession session = req.getSession(true);
storage = (Storage) session.getAttribute(Storage.class.getName());
if (storage == null) {
storage = new Storage();
session.setAttribute(Storage.class.getName(), storage);
}
// create odata handler and configure it with CsdlEdmProvider and Processor
OData odata = OData.newInstance();
ServiceMetadata edm = odata.createServiceMetadata(new PersonEdmProvider(), new ArrayList<EdmxReference>());
ODataHttpHandler handler = odata.createHandler(edm);
handler.register(new PersonEntityCollectionProcessor(storage));
handler.register(new PersonEntityProcessor(storage));
handler.register(new PersonPrimitiveProcessor(storage));
// let the handler do the work
handler.process(req, resp);
} catch (RuntimeException e) {
LOG.error("Server Error occurred in ExampleServlet", e);
throw new ServletException(e);
}
}
}
PersonDAO
@Stateless
public class PersonDAO {
@PersistenceContext
private EntityManager em;
public List<Person> getAllPersons() {
return
em.createQuery("SELECT p FROM T_Person p", Person.class).getResultList();
}
}
ストレージ:
@Stateless
public class Storage {
@EJB
PersonDAO psDAO;
private List<Entity> personList;
public Storage() {
personList = new ArrayList<Entity>();
}
@PostConstruct
private void initSampleData(){
psDAO.getAllPersons();
}
}
PersonEntityCollectionProcessor:
public class PersonEntityCollectionProcessor implements EntityCollectionProcessor {
private OData odata;
private ServiceMetadata serviceMetadata;
private Storage storage;
public PersonEntityCollectionProcessor(Storage storage) {
this.storage = storage;
}
public void init(OData odata, ServiceMetadata serviceMetadata) {
this.odata = odata;
this.serviceMetadata = serviceMetadata;
}
}
が提供する回答のいずれかがあなたの問題を解決しました、それを再作成、サーブレットからストレージの実際のSessionBeanのを使用することはありませんか? – AceFunk