javajsf-2java-ee-6cdiweld

CDI bean configuration using beans.xml file


I have very simple CDI bean:

package net.resourceAuth;

public class Sample {

   private String text;

   public String getText() {
    return text;
   }

   public void setText(String text) {
    this.text = text;
   }
}

And now I would like to initialize text variable using beans.xml. I'm trying with beans.xml file like this one:

<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:res="urn:java:net.resourceAuth"
    xsi:schemaLocation="
    http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">

    <res:Sample>
      <res:text>test123</res:text>
    </res:Sample>

</beans>

But it does not work. text is always null. Can You help me figure out what is wrong here?

In other words: I am looking for a similar solution as it is used in the JSF faces-config.xml described for example here: http://www.mkyong.com/jsf2/configure-managed-beans-in-jsf-2-0/


Solution

  • There is no built-in solution for this problem. You can use some third party solutions like Apache DeltaSpike http://deltaspike.apache.org/ or implement it by Your own using CDI extensions for example.