[英]CDI bean configuration using beans.xml file
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 text
variable using beans.xml
. 现在,我想使用
beans.xml
初始化text
变量。 I'm trying with beans.xml
file like this one: 我正在尝试使用像这样的
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. 但这行不通。
text
is always null. text
始终为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/ 换句话说:我正在寻找类似的解决方案,例如在此处描述的JSF faces-config.xml中使用的解决方案: 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.
您可以使用某些第三方解决方案,例如Apache DeltaSpike http://deltaspike.apache.org/,也可以使用CDI扩展名自己实现。
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). 我真的不知道可以在
beans.xml
这种配置(这可能仅在Spring中有效,但也许有人会纠正我)。 The CDI way of initializing the values is the method annotated with @PostConstruct
, so try this 初始化值的CDI方法是
@PostConstruct
注释的方法,因此请尝试此方法
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 ? 您是否尝试过实现像这样的@Observes ProcessInjectionTarget的javax.enterprise.inject.spi.Extension来包装InjectionTarget ?
This example inject bean values from resource bundle. 本示例从资源束中注入bean值。
如果看到.xml文件,则在发布的示例链接中,它使用包名,然后使用类名,因此,如果尝试从<ress:Sample>
更改为<ress:net.resourceAuth.Sample>
它可能会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.