最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

scopes - @PostConstruct method not called when RequestScoped bean injected in ApplicationScoped bean - Stack Overflow

programmeradmin5浏览0评论

I have the following Producer class:

@RequestScoped
public class MsmEntityManagerProducer {

    @PersistenceContext(unitName = MSM)
    private EntityManager entityManager;

    private static final String MSM = "MSM";

    @Produces
    @MsmQualifier
    public EntityManager getMsmEntityManager() {
        return entityManager;
    }
}

which is used in a class that should execute on JBoss startup:

@ApplicationScoped
public class StartupActiviti {

    @Inject
    @MsmQualifier
    private EntityManager entityManager;

    public void init(@Observes @Initialized(ApplicationScoped.class) Object startupObject){
    }

    @PostConstruct
    public void updateTasks() {
        log.debug("start StartupActiviti");
    }
}

However, the updateTasks() method is not called and silently ignored without any exception. Well, I have found a solution (either change @RequestScoped to @Dependent or retrieve the EntityManager within the @PostConstruct method instead of injection, but does anybody has an explanation for this weird behaviour? Such kind of failures are really hard to track down if no exception is raised

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论