문제

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/

도움이 되었습니까?

해결책

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.

다른 팁

I am really not aware that this kind of configuration can be made in beans.xml (this probably works just in Spring but maybe somebody will correct me). The CDI way of initializing the values is the method annotated with @PostConstruct, so try this

public class Sample {

    private String text;

    @PostConstruct
    public void init() {
         this.text = "aaa";
    }
}

Have you try to implement a javax.enterprise.inject.spi.Extension that @Observes ProcessInjectionTarget like this Wrapping an InjectionTarget ?

This example inject bean values from resource bundle.

In the sample link that you posted if you see the .xml file it uses the package name and then the class name so if you try to change from <ress:Sample> to <ress:net.resourceAuth.Sample> it might work.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top