Why is PostConstruct not called?

余生颓废 提交于 2019-12-23 07:19:45

问题


I am working on a simple Java EE application.

I have class like this:

import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

@Stateless
public class BlogEntryDao {

    EntityManager em;

    @PostConstruct
    public void initialize(){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Persistence");
        em = emf.createEntityManager();
    }

    public void addNewEntry(){
        Blogentry blogentry = new Blogentry();

        blogentry.setTitle("Test");
        blogentry.setContent("asdfasfas");

        em.persist(blogentry);

    }
}

So my managed bean calls this method. Until here no problems. But since the initialize method is not called, I am getting an NPE in em.persist.

Why is the initialize method not being called? I am running this on Glassfish server.

Regards.


回答1:


The Java EE bean annotations such as @PostConstruct only apply to container-managed beans. If you are simply calling new BlogEntryDao yourself, the container isn't going to intercept the creation and call the @PostConstruct method.

(Furthermore, you'd be better off using @PersistenceContext or @PersistenceUnit instead of manually fetching the EntityManagerFactory in your initialize() method, and you should be creating an EntityManager for each call to addNewEntry(), since they're short-lived. Making these changes would eliminate the need for initialize() at all.)




回答2:


Since this question comes up first on Google for "postconstruct not called", another reason a @PostConstruct method might not be called besides using the new keyword instead of putting @PostConstruct in a Spring bean is if you have a circular dependency.

If this bean were to depend on another bean that depended on this bean, your other bean might call addNewEntry() before BlogEntryDao was initialized, even though BlogEntryDao is a dependency for that other bean.

This is because Spring didn't know which bean you wanted to load first due to the circular reference. In this case, one can remove the circular reference or use @AutoWired/@Value constructor parameters instead of member values or setters, or if using xml configuration, maybe you can swap the order in which the beans are defined.




回答3:


I had the same problem in my application. You didn't post your bean context configuration xml file (so I'm not sure if it's the same issue) but in my case adding this line:

<context:annotation-config/>

Solved my problem. You need either <context:annotation-config/> or <context:component-scan/> to enable @PostConstruct annotation.




回答4:


In my case @PostConstruct was not called because my initialize() method was static and was also throwing an exception. In either case the method is ignored. I hope it helps someone else who made the same mistake. This can be found in the console:

WARNING: JSF1044: Method '<XXX>' marked with the 'javax.annotation.PostConstruct' annotation cannot be static.  This method will be ignored.
WARNING: JSF1047: Method '<XXX>' marked with the 'javax.annotation.PostConstruct' annotation cannot declare any checked exceptions.  This method will be ignored.


来源:https://stackoverflow.com/questions/18161682/why-is-postconstruct-not-called

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!