Java 使用 beans.xml 文件配置 CDI bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19159918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:36:21  来源:igfitidea点击:

CDI bean configuration using beans.xml file

javajsf-2java-ee-6cdiweld

提问by pWoz

I have very simple CDI bean:

我有非常简单的 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 textvariable using beans.xml. I'm trying with beans.xmlfile like this one:

现在我想text使用beans.xml. 我正在尝试使用这样的beans.xml文件:

<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. textis always null. Can You help me figure out what is wrong here?

但它不起作用。text始终为空。你能帮我弄清楚这里出了什么问题吗?

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/

换句话说:我正在寻找一个类似的解决方案,因为它在此处描述的 JSF faces-config.xml 中使用:http: //www.mkyong.com/jsf2/configure-managed-beans-in-jsf- 2-0/

采纳答案by pWoz

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.

这个问题没有内置的解决方案。您可以使用一些第三方解决方案,例如 Apache DeltaSpike http://deltaspike.apache.org/,或者您自己使用 CDI 扩展来实现它。

回答by Petr Mensik

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

我真的不知道可以进行这种配置beans.xml(这可能只适用于 Spring,但也许有人会纠正我)。初始化值的 CDI 方法是用 注释的方法@PostConstruct,所以试试这个

public class Sample {

    private String text;

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

回答by jboz

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

你有没有尝试实现一个 javax.enterprise.inject.spi.Extension ,@Observes ProcessInjectionTarget 像这样Wrapping an InjectionTarget

This example inject bean values from resource bundle.

此示例从资源包中注入 bean 值。

回答by Dimitris

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.

在您发布的示例链接中,如果您看到 .xml 文件,它使用包名,然后使用类名,因此如果您尝试从 更改<ress:Sample><ress:net.resourceAuth.Sample>它可能会起作用。