JSF application scope instantiation and injection

后端 未结 2 1015
鱼传尺愫
鱼传尺愫 2021-02-20 08:11

Probably my question is a trivial one, but I never used an application scope bean before. I need the application bean because I have to do time consuming transactions on the dat

相关标签:
2条回答
  • 2021-02-20 08:32

    Since I had the same problem, and I used @BalusC code to solve it, I feel there is maybe just something to add here.

    Question author stated that:

    After I read that if I want to inject the application bean into another bean I have to use @PostConstuct annotation

    This was exactly my problem. I did everything like BalusC did in his answer, except I didn't have Application Bean used in any manner in Session Bean's init() method. So, I added

    @ManagedBean
    @SessionScoped
    public class TestsBean implements Serializable {
    
    @PostConstruct 
    init(){
    ....
    System.out.println("appbean session init size" + appbean.getPinovi().size());
    ....
    } 
    

    This is what I had to do to get it working, just put appBean into use somehow. It looks like hack, though...

    0 讨论(0)
  • 2021-02-20 08:37

    There are 2 potential mistakes.

    First, the @ManagedBean(eager=true) works, as its javadoc says, only on application scoped JSF managed beans. So it works only when you have used @ApplicationScoped of javax.faces.bean package (and thus not javax.enterprise.context package!). The eager=true basically means that the bean will be auto-instantiated on webapp's startup instead of only later when it's referenced for the first time in EL.

    Second, the managed bean name defaults to the classname in decapitalized form according the Javabeans specification. You didn't explicitly specify any managed bean name like @ManagedBean(name="container", eager=true), so the managed bean name will default to applicationContainer, however yet you're still attempting to reference it as #{container} instead of #{applicationContainer}.

    You are not clear at all on which problems/errors you're facing. If you're getting an exception, you should absolutely read/interpret it and if you're unable to understand it, copypaste it in its entirety -including the stacktrace- in the question. It represents namely the whole answer to your problem at its own. You just have to interpret and understand it (or we just have to explain it in layman's terms). You should really not ignore them and leave them out the question as if they are irrelevant decoration. They are not!

    All with all, the complete and proper approach would be, complete with the import declarations just to be sure, and also some poor man's stdout prints for debugging:

    package com.example;
    
    import javax.faces.bean.ApplicationScoped;
    import javax.faces.bean.ManagedBean;
    
    @ManagedBean(eager=true)
    @ApplicationScoped
    public class ApplicationContainer {
    
        public ApplicationContainer() {
            System.out.println("ApplicationContainer constructed");
        }
    
    }
    
    package com.example;
    
    import java.io.Serializable;
    import javax.annotation.PostConstruct;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ManagedProperty;
    import javax.faces.bean.SessionScoped;
    
    @ManagedBean
    @SessionScoped
    public class TestsBean implements Serializable {
    
        @ManagedProperty("#{applicationContainer}")
        private ApplicationContainer container;
    
        public TestsBean() {
            System.out.println("TestsBean constructed");
        }
    
        @PostConstruct
        public void init() {
            System.out.println("ApplicationContainer injected: " + container);
        }
    
        public void setContainer(ApplicationContainer container) {
            this.container = container;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题