EJB @PostConstruct未调用 [英] EJB @PostConstruct not called

查看:157
本文介绍了EJB @PostConstruct未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想使用@PostConstruct调用一个方法时,不会调用该方法。我没有从服务器收到任何错误或日志。我必须添加一些配置xml文件或添加其他注释来调用方法?

When I want to invoke a method with @PostConstruct, the method is not invoked. I don't get any errors or logs from the server. Do I have to add some configuration xml files or add additional annotations to invoke the method?

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

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();

    }

}

存储: / p>

Storage:

@Stateless
public class Storage {

    @EJB
    PersonDAO psDAO;

    private List<Entity> personList;

    public Storage() {
        personList = new ArrayList<Entity>();
    }

    @PostConstruct
     private void initSampleData(){
         psDAO.getAllPersons();
    }
}

PersonEntityCollectionProcessor:

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

}


推荐答案

我认为这个问题是你代码的一部分:

I think the problem is this part of your code:

    storage = (Storage) session.getAttribute(Storage.class.getName());
    if (storage == null) {
       storage = new Storage();
       session.setAttribute(Storage.class.getName(), storage);
    }    

通过这个创建一个非管理的存储而不是使用注入的实例,因此不会调用 @PostConstruct
如果通过 @EJB 注入存储的实例是 null 执行此代码时,您可能会遇到注入问题。

With this you create a non-managed instance of the Storage instead of using the injected instance and therefore the @PostConstruct is not called. If the instance of Storage injected via @EJB is null when this code is executed, you may have a problem with the injection.

您可以删除这5行,它应该可以正常工作。您不需要在会话中手动保存实例。

You can remove these 5 lines and it should work. You don't need to save instance manually in the session.

这篇关于EJB @PostConstruct未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆