简体   繁体   中英

JSF @ManagedProperty not being injected

I am writing a web app and I am using JSF 2.0 and hibernate 3.5. My problem is when I would like to inject a bean it's not being injected.

This is the class I would like to inject(I copied only the important parts):

@ManagedBean(name="permissionBean", eager = true)
@SessionScoped
public class PermissionBean {
    private List<Role> rolesList;
    public PermissionBean(){
        refresh();
    }

    private void refresh(){
        rolesList = Role.queryRolesList();
    }
    public void test(){
        System.out.println("__________TEST ");
    }
//getter setter for the rolesList
}

Into this:

@ManagedBean(name="triggerBean")
@SessionScoped
public class Trigger extends EmptyInterceptor {

    @ManagedProperty(value="#{permissionBean}")
    public PermissionBean pb;
    public void onDelete(..) {
        pb.test();
    }
    public void setPb(PermissionBean pb) {
        System.out.println("______setting bean");
        this.pb = pb;
    }
}

The second class is an interceptor class for hibernate. The program starts and works, I am sure the PermissionBean is being constructed because I am using the methods in it, I am seeing the list. But when I try to delete from it, and the onDelete() function triggers I got a null pointer exception(on that line where I call pb.test() ). After some trying I put a test write out into the setter, but that method never gets called.

I guess the eager=true is the problem. 'Eager true' only makes sense in an application scoped bean, i don't know what effect it has in a session scoped bean as it was not designed for that. But from your describtion, it seems that it has the effect that one object is being created when you start the application but this object is not in the same session context as that object of the Trigger bean from which you are trying to access it. As long as the PermissionBean is not referenced from any xhtml page the managedProperty inside the Trigger Class will remain null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM