You can go 2 ways about this: one easy, the other one more in line with the general JSF philosophy. In other words it's a matter of personal integrity to technology principles :D
The easy one is the one illustrated by Per Henrik Lausten. However it's flawed in a way. It isn't really environment neutral/agnostic. It doesn't mean it's a problem but if you want to redistribute your bean you are forced, and are forcing, you, your implementor, to go by the bean name of your choosing - e.g. appBean
Now I don't want to overcomplicate things but a more flexible approach would be the following:
<managed-bean>
<managed-bean-name>appBean</managed-bean-name>
<managed-bean-class>bean.ApplicationBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>name</property-name>
<value>appBean</value>
</managed-property>
</managed-bean>
The bean:
public class ApplicationBean implements Serializable {
private static final long serialVersionUID = 1L;
private static final String DEFAULT_NAME = "appBean";
private String name;
private boolean godMode;
public void setName(String name) {
this.name = name;
}
public void setGodMode(boolean godMode) {
this.godMode = godMode;
}
public boolean isGodMode() {
return godMode;
}
public static ApplicationBean getInstance(FacesContext facesContext) {
return (ApplicationBean) Helper.resolveVariable(facesContext, name != null ? name : DEFAULT_NAME);
}
}
With the above approach the bean can be deployed anywhere and if necessary you can define a different name - managed-bean-name
and the property value MUST match. Otherwise you omit the name property in the faces-config.xml while setting the managed-bean-name
exactly as it appears in the DEFAULT_NAME
java class property.
The "philosophical" way instead is the one of leveraging the attribute injection. It all depends on how often you find yourself needing a particular bean across all your beans. But see the example first:
<managed-bean>
<managed-bean-name>appBean</managed-bean-name>
<managed-bean-class>bean.ApplicationBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>godMode</property-name>
<value>true</value>
<property-class>java.lang.Boolean</property-class>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>myOtherBean</managed-bean-name>
<managed-bean-class>bean.MyOtherBean</managed-bean-class>
<managed-bean-scope>view</managed-bean-scope>
<managed-property>
<property-name>app</property-name>
<value>#{appBean}</value>
</managed-property>
</managed-bean>
The other bean class:
public class MyOtherBean implements Serializable {
private static final long serialVersionUID = 1L;
private ApplicationBean app;
public void setApp(ApplicationBean app) {
this.app = app;
}
public void doSomething() {
if (app.isGodMode()) {
}
}
}
I personally use both approaches, although I prefer this last approach. Theres a catch though: you can only inject beans that share the same scope or that outlive the scope of the bean you are injecting them in.
app into session: YES
session into view: YES
view into app: NO
request into view: NO