2016-08-01 54 views
0

我有这个@Startup EJB其读取和写入一个属性文件类路径上的不同的应用配置变量:启动EJB不注射时SessionScope CDI Bean的初始化

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

它然后注入会话范围CDI豆,其与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构造证实我的怀疑 - EJB是对CDI建设空。但是,如果我定位到上面提到的UI网页并调用updateUriProperties方法的EJB不再为空:

<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中的用户更新。但是,我使用UriSetterBean的getJavascriptLink()属性访问器在角色ADMIN中的用户使用的另一个页面中。 getJavascriptLink()输出休息服务的基础URL。

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

+0

注射发生后** **构造函数被调用。这是你的问题吗?然后使用@PostConstruct注释的方法。如果不是,我觉得很难理解你真正的问题是什么。 – Kukeltje

+0

谢谢!听起来像是这样,但我该如何使用它?我不能在init方法中执行新的CompanyPropertiesImpl()(用PostConstruct注释)。在应用程序启动时创建所有EJB之后。我可以从某处强行检索它吗?你可以把你的评论放在'答案'中。你明白了。 – vasigorc

+0

不需要做一个'新',只是尝试。 ejb将在那里 – Kukeltje

回答

0

解决由Kukeltje

通过将@PostConstruct标注的方法,并从EJB retirieving所需的值:

@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"; 
    }