2011-10-11 56 views
0

我尝试注入一个applicationScoped Bean。 我发现了类似的主题在JSF2 ApplicationScope bean instantiation time?Get JSF managed bean by name in any Servlet related classJava Inject Applicationscope Bean

随着faces上下文JSF的方式,所有的罚款(WebsitesController是AppScoped):

FacesContext context = FacesContext.getCurrentInstance(); 
WebsiteController websitesController = context.getApplication().evaluateExpressionGet(context, "#{websitesController}", WebsitesController.class); 

随着两个溢出线程这是行不通的注射。我的代码:

@ManagedBean(eager=true) 
@ApplicationScoped 
public class WebsitesController implements Serializable { 
...} 

,现在我想

@ManagedBean(name = "shopController") 
@ViewScoped 
public class ShopController { 
    {Injection-Statement} 
    private WebsitesController websitesController; 

我尝试下面的语句:

@ManagedProperty("#{websitesController}") 
@Inject 
@EJB 

什么我的错吗?

回答

1

我也有Glassfish的ApplicationScope的问题。你有一个maven或ant webproject? (使用Maven我觉得作为预期ApplicationScope doesn't工作 - 蚂蚁它)

现在你的问题:

当您使用@注入,那么你需要WebsiteController和@Named @ApplicationScope(其它进口作为jsf !!),你必须有一个beans.xml - 然后CDI被激活。

当您使用@EJB时,则WebsiteController需要@Stateless。我想我可以帮你...