来自另一个托管bean的托管bean的访问权限属性 [英] Cant access property of managed bean from another managed bean

查看:66
本文介绍了来自另一个托管bean的托管bean的访问权限属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用@ManagedProperty访问另一个bean中的@SessionScoped bean的属性.简而言之,我想在secondBean中访问firstBean的name属性.

I want to access the property of a @SessionScoped bean in another bean using @ManagedProperty. In short, I want to access the name property of firstBean in secondBean.

@ManagedBean
@SessionScoped
public class FirstBean implements Serializable{
    private String name;
    //...other attributes
    //...constructor
    public String getSelectedModel() {
        return selectedModel;
    }

    public void setSelectedModel(String selectedModel) {
        this.selectedModel = selectedModel;
    }
    //other getters&setters
}

第二个bean:

@ManagedBean
@SessionScoped
public class SecondBean implements Serializable{

@ManagedProperty(value="#{firstBean}")
private FirstBean firstBean

public SecondBean() {
    System.out.println(firstBean.getName());
}
public IndexBean getFirstBean() {
    return firstBean;
}

public void setFirstBean(FirstBean firstBean) {
    this.firstBean = firstBean;
}

运行此命令时,总是在第二个bean的构造函数中的System.out.println(firstBean.getName());上获得NullPointerException,这似乎意味着我需要创建firstBean的新实例.

When I run this, I always get NullPointerException on System.out.println(firstBean.getName()); in the constructor of second bean, which seems to mean that I need to create a new instance of firstBean.

但是奇怪的是,当我注释掉这一行时,我可以做这样的事情而没有错误,这意味着firstBean实际上是secondBean的属性.

But strangely, when I commented out this line, I can do something like this with no errors, which means that firstBean is actually a property of secondBean.

<h:outputText value="#{secondBean.firstBean.name}" />

这是什么问题?

推荐答案

无法在构造函数中访问注入的依赖项.您基本上希望Java能够执行以下操作:

It's not possible to access an injected dependency in the constructor. You're basically expecting that Java is able to do something like this:

SecondBean secondBean; // Declare.
secondBean.firstBean = new FirstBean(); // Inject.
secondBean = new SecondBean(); // Construct.

如果尚未构造实例,则绝对不可能设置实例变量.相反,它的工作方式如下:

It's absolutely not possible to set an instance variable if the instance is not constructed yet. Instead, it works as follows:

SecondBean secondBean; // Declare.
secondBean = new SecondBean(); // Construct.
secondBean.firstBean = new FirstBean(); // Inject.

然后,为了基于注入的依赖关系执行业务操作,请使用带有@PostConstruct注释的方法.构造依赖项注入后,依赖关系注入管理器将直接调用它.

Then, in order to perform business actions based on injected dependencies, use a method annotated with @PostConstruct. It will be invoked by the dependency injection manager directly after construction and dependency injection.

所以,只需替换

public SecondBean() {
    System.out.println(firstBean.getName());
}

作者

@PostConstruct
public void init() { // Note: method name is fully to your choice.
    System.out.println(firstBean.getName());
}

这篇关于来自另一个托管bean的托管bean的访问权限属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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