Question

I'd like to initiliaze the properties of a bean (application scope) from the faces-config.xml. I tried different configuration without success. At library level I'm using jsf 2.2 - jboss-jsf-api_2.2_spec.jar. At project level faces-config is configured to 2.0 version. I don't know if that is the problem. JBDS 7 don't let me to change to 2.2 beacouse of conflict with others Project Facets.

This is the faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">      
    <managed-bean>
        <managed-bean-name>appBean</managed-bean-name>
        <managed-bean-class>package.ApplicationBean</managed-bean-class>
        <managed-bean-scope>application</managed-bean-scope>
        <managed-property>
            <property-name>cookieNameLocale</property-name>
            <property-class>java.lang.String</property-class>
            <value>someText</value>
        </managed-property>
        <managed-property>
            <property-name>debug</property-name>
            <property-class>boolean</property-class>
            <value>true</value>
        </managed-property>
    </managed-bean>
    <application>
        <locale-config>
            <default-locale>xx_XX</default-locale>
            <supported-locale>xx_XX</supported-locale>
        </locale-config>
        <resource-bundle>
            <base-name>locale</base-name>
            <var>i18n</var>
        </resource-bundle>
     </application>
</faces-config>

This is the application scope bean:

public class ApplicationBean implements Serializable {

    private boolean debug;
    private String cookieNameLocale;

    //respectively getters and setters
}

When @Inject the appBean into another session scope bean the properties are not initiliazed. There are not errors and appBean is created before session bean (using @PostConstruct to print)

Was it helpful?

Solution

The <managed-bean> entry in faces-config.xml basically declares a new @ManagedBean. I.e. a JSF managed bean. However, with @Inject you're basically injecting a CDI managed bean.

Those are two mutually exclusive ways of managing beans. Effectively, you end up with 2 instances of the very same bean class, one managed by JSF via faces-config.xml and another one managed by CDI via annotations. Only the one managed by JSF has those properties set.

You've 2 options:

  1. Use @ManagedProperty to inject it as a JSF managed bean. This in turn however requires that the acceptor is by itself also a JSF managed bean.

  2. Forget the faces-config.xml approach altogether. Define them as JNDI resources in either web.xml or server config and use @Resource to inject them. Alternatively, define them as .properties file settings or <context-param> entries in web.xml. CDI doesn't offer ways out the box to inject them, but it's possible to create a custom annotation with a CDI Producer for that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top