Search code examples
javajsfcdimanaged-beanjboss-weld

Converting JSF with managed-property to CDI


We have a very complex config file for JSF managed beans that looks like the below.
Is there any way to convert this to some kind of CDI config?
Now when we use @Inject on AbcConfigFactory it doesn't initialise the values from the config file. I guess this is because we use CDI to initialise it and not JSF... or something like that. :)

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="1.2"
   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_1_2.xsd">

   <managed-bean>
      <managed-bean-name>AbcConfig</managed-bean-name>
      <managed-bean-class>com.a.b.c.AbcConfigFactory</managed-bean-class>
      <managed-bean-scope>application</managed-bean-scope>
      <managed-property>
         <property-name>abcSites</property-name>
         <list-entries>
            <value-class>com.a.b.c.SiteConfigBean</value-class>
            <value>#{SiteConfig$A}</value>
            <value>#{SiteConfig$B}</value>
         </list-entries>
      </managed-property>
   </managed-bean>


   <managed-bean>
      <managed-bean-name>SiteConfig$A</managed-bean-name>
      <managed-bean-class>com.a.b.c.SiteConfigBean</managed-bean-class>
      <managed-bean-scope>none</managed-bean-scope>
      <managed-property>
         <property-name>siteName</property-name>
         <value>A</value>
      </managed-property>

      <managed-property>
         <property-name>starConfig</property-name>
         <property-class>com.a.b.c.StarConfigBean</property-class>
         <value>#{StarConfig$A}</value>
      </managed-property>
   </managed-bean>

   <managed-bean>
      <managed-bean-name>SiteConfig$B</managed-bean-name>
      <managed-bean-class>com.a.b.c.SiteConfigBean</managed-bean-class>
      <managed-bean-scope>none</managed-bean-scope>
      <managed-property>
         <property-name>siteName</property-name>
         <value>B</value>
      </managed-property>

      <managed-property>
         <property-name>starConfig</property-name>
         <property-class>com.a.b.c.StarConfigBean</property-class>
         <value>#{StarConfig$A}</value>
      </managed-property>
   </managed-bean>

   <managed-bean>
      <managed-bean-name>StarConfig$A</managed-bean-name>
      <managed-bean-class>com.a.b.c.StarConfigBean</managed-bean-class>
      <managed-bean-scope>none</managed-bean-scope>
      <managed-property>
         <property-name>siteName</property-name>
         <value>A</value>
      </managed-property>
   </managed-bean>
</faces-config>

Solution

  • The initial version of the CDI spec had XML-configuration beans in it, but it was removed later, just to be re-included in future versions of the spec recently (AFAIK).

    So, the short answer is: No, you cannot use XML to configure CDI beans (and as CDI and JSF are two different things, you wouldn't be able to re-use a JSF config file without changes - obviously).

    The solution to your problem comes with Seam Solder which uses the extension mechanism of CDI to add exactly that functionality (and much more). But you will still have to re-work your configuration in order to fit for your CDI-beans.

    Without knowing anything about your problem domain:

    If XML-configuration is some sort of historic relict that is not forced by a real-world requirement (like having different wirings for different deployment scenarios), I strongly recommend to dump the XML and switch to CDI's typesafe annotation-based approach. There are concepts for all mainstream usecases (like different wiring for testing), and you are freed from the burden of doing your "coding" in XML.

    If you really need (or still want) XML-based configuration, Seam Solder is your way to go.