我想使用CDI向JSF Web应用程序中的托管bean注入常量字符串消息,这里是生产者类:
@Named
@RequestScoped
public class StringProducer {
@Produces
@Named("message")
@RequestScoped
public String getMessage() {
return "Hello World";
}
}
Run Code Online (Sandbox Code Playgroud)
以下是它如何在另一个托管bean中注入:
@Inject Named("message") String message;
Run Code Online (Sandbox Code Playgroud)
但这总是导致异常:
org.jboss.weld.exceptions.UnproxyableResolutionException: WELD-001435 Normal scoped bean int is not proxyable
Run Code Online (Sandbox Code Playgroud)
我试图在Instance中包装String类型,如下所示:
@Inject Named("message") Instance<String> message;
Run Code Online (Sandbox Code Playgroud)
但没有改变.
问题是你@RequestScoped在producer方法上使用了注释.删除它,应用程序将按预期工作.
该请求范围注释用于标注豆类由容器管理.为此,容器代理对象的公共方法.然而,像String这样的最终类是不可代理的,正如使用Weld 2.0.0 SP1在Glassfish 4.0上运行代码时的异常所指出的:
WELD-001437 Normal scoped bean class java.lang.String is not proxyable because the type is final or it contains a final method class java.lang.String - Producer Method [String] (...etc.)
Run Code Online (Sandbox Code Playgroud)
简短的信息为未来的读者:
除了四个内置作用域之外,CDI还支持两个伪作用域:
@Singleton@Dependent以上两个伪范围都有一些有趣的特性:CDI不会为它们创建代理对象.
因此,所有不可代理的类(例如由于是最终的或由于缺少无参数的公共构造函数)都可以标记@Singleton或@Dependent- 当然,如果它是有意义的.
| 归档时间: |
|
| 查看次数: |
2512 次 |
| 最近记录: |