在初始化SessionScope CDI Bean时未注入启动EJB

时间:2016-08-01 00:47:51

标签: jsf ejb cdi

我有这个@Startup EJB,它读取和写入类路径上不同的应用程序配置变量的.properties文件:

@Singleton
@Startup
@Production
public class CompanyPropertiesImpl implements CompanyProperties {
...
 public CompanyPropertiesImpl() {
    uriProperties = PropertiesProvider.getUriProperties();
    ...
}

然后将其注入Session Scoped CDI Bean,它与UI网页交互:

@Named(value = "companyPropertiesBean")
@SessionScoped

public class UriSetterBean implements Serializable {
...

@Inject
@Production
private CompanyProperties companyProperties;

public UriSetterBean() {
    this.urisUpdated = false;
    this.updateTried = false;
    serviceSuffix = "/RimmaNew/rest";
    if (companyProperties==null) {
        System.out.println("true");
    }else{
        System.out.println("false");
    }
}

public void setCompanyProperties(CompanyProperties companyProperties) {
    this.companyProperties = companyProperties;
}
 ...
public void updateUriProperties() {
    if (hostName == null || hostName.equals("") || schemeName == null || schemeName
            .equals("")) {
        updateTried = true;
        urisUpdated = false;
    } else {
        companyProperties.setHostName(hostName);
        companyProperties.setSchemeName(schemeName);
        if (valueOf(port) == null || port != 0) {
            companyProperties.setPort(port);
        } else //i.e. port is not part of the uri
        {
            companyProperties.setPort(-1);
        }
        updateTried = true;
        urisUpdated = true;
    }
...
public String getJavascriptLink() {
    updateJavascriptLink();
    return javascriptLink;
}

我已将if语句粘贴到UriSetterBean构造函数中以确认我的怀疑 - 在CDI构造中EJB为null。但是,如果我导航到上面提到的UI网页并调用updateUriProperties方法,则EJB不再为null:

<h:commandButton action="#{companyPropertiesBean.updateUriProperties()}" class="btn btn-default" id="submitUriChanges"
                                     style="margin-top: 0.5em" value="#{bigcopy.submitUriChanges}"
                                     type="button">
                        <f:ajax event="click" render="updateUriStatus" execute="@form"></f:ajax>
                    </h:commandButton>

enter image description here

但这对我来说不是一个可以接受的解决方案。这就是原因。

UriSetterBean仅应由角色SUPERUSER中的用户更新。但是我在角色ADMIN中的用户使用的另一个页面中使用UriSetterBean的getJavascriptLink()属性访问器。 getJavascriptLink()输出休息服务的基本URL。

如何在UriSetterBean初始化时“强制”初始化EJB CompanyPropertiesImpl?感谢您的关注和考虑。我意识到这可能不是微不足道的。

1 个答案:

答案 0 :(得分:0)

Kukeltje

解决

添加@PostConstruct注释方法并从EJB中重新提供所需的值:

@Named(value = "companyPropertiesBean")
@SessionScoped
public class UriSetterBean implements Serializable {

    private String hostName, schemeName, serviceSuffix, javascriptLink;
    private int port;
    ...

    @Inject
    @Production
    private CompanyProperties companyProperties;

    //THE ADDED METHOD
    @PostConstruct
    public void init(){
        setHostName(companyProperties.getHostName());
        setSchemeName(companyProperties.getSchemeName());
        setPort(companyProperties.getPort());
    }

    public UriSetterBean() {
        this.urisUpdated = false;
        this.updateTried = false;
        serviceSuffix = "/RimmaNew/rest";
    }
相关问题