简体   繁体   中英

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 );

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM