JSF2 Managed Bean Reference Problem: CDI Injection?
I have a managed bean called:
@ManagedBean(name="configBean")
@SessionScoped
public class configBean implements Serializable {
that instantiates a class/bean (that isn't a managed bean its a standard class):
com.package.class variableName = new com.package.class();
& a number of objects are created/set from this class eg:
variableName.setCached( true );开发者_JS百科
And I have another bean, which at the moment is simply called:
@ManagedBean(name="testBean")
@SessionScoped
public class testBean implements Serializable {
& basically I want to reference/implement the 'variableName
' instantiation in my testBean like so:
if( !( variableName.isCached() ) )
{
System.out.println( "cry yourself to sleep foo..");
}
else
{
System.out.println( "your not as useless as you look");
}
From what I've seen it looks as though Bean Injection is what I am looking for? However I haven't got it working yet so was hoping someone could knock-up a quick example so I know I am on the right lines!
Cheers
Since both configBean
and testBean
are managed beans you can reference them like this:
@ManagedBean(name="testBean")
@SessionScoped
public class testBean implements Serializable {
@ManagedProperty(value="#{configBean}")
private ConfigBean configBean;
....
... configBean.getVariableName().isCached()...
}
Following code in testBean
private configBean configBean;
@ManagedProperty(value="configBean")
public configBean getConfigBean()
{
return configBean;
}
public void setConfigBean(configBean configBean)
{
this.configBean = configBean;
}
Then you can access variableName with
configBean.getVariableName();
精彩评论