I am struggling a bit with using one Java class in another class.
For my application I have set up a managed bean for a utility class:
<managed-bean>
<managed-bean-name>utilBean</managed-bean-name>
<managed-bean-class>com.acme.domino.utils.Utils</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>debug</property-name>
<value>false</value>
<property-class>java.lang.Boolean</property-class>
</managed-property>
<managed-property>
<property-name>log</property-name>
<value>false</value>
<property-class>java.lang.Boolean</property-class>
</managed-property>
</managed-bean>
In this class has a method to write to a log.
Now I would like to use this class in another class. So my thoughts are to inject the managed bean above in another managed bean for the other class.
<managed-bean>
<managed-bean-name>orderBean</managed-bean-name>
<managed-bean-class>com.acme.domino.app.Order</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>utils</property-name>
<value>#{utilsBean}</value>
</managed-property>
</managed-bean>
The Order class has as field
public Utils utils;
public Utils getUtils() {
return utils;
}
public void setUtils(Utils utils) {
this.utils = utils;
}
But in the constructor it goes already wrong:
public Order() throws Exception {
super();
utils = new Utils();
System.out.println("log?? " + utils.log);
..}
It seems that in the constructor of the Utils class I have to set the two fields somehow:
public Utils() {
this.debug = true;
this.log = true;
}
Within the Utils class the values for the fields are false (as set via the managed properties.
Within the Orders class the values for the fields are true (as set via Utils constructor).
Is there a way to avoid to set the fields in the Utils constructor? Or is there away to avoid to define a new instance of the Utils class within the Order class?
The message that I get when I do not set the fields in the Utils constructor class is:
javax.faces.FacesException: Can't instantiate class: 'com.acme.domino.app.orderBean'.. null Can't instantiate class: 'com.acme.domino.app.orderBean'.. null class com.acme.domino.app.orderBean : java.lang.NullPointerException
I would like to have that my Order class inherits the settings of the Utils class set via the Managed Bean.
instead of :
utils = new Utils();
try:
Utils utils = (Utils)ExtLibUtil.resolveVariable(FacesContext.getCurrentInstance(), "utilBean");
more background on reading managed bean from java: XPages: How to acces an application scope bean from a session scope bean