Inject producer method that returns String CDI

…衆ロ難τιáo~ 提交于 2019-12-05 02:37:35

The problem is your use of the @RequestScoped annotation on the producer method. Remove it and the application will work as expected.

The Request Scoped annotation is used to annotate Beans managed by the container. To do so, the container proxies the object's public methods. Final classes like String are however not proxyable, as pointed out by the exception when running the code on Glassfish 4.0 with Weld 2.0.0 SP1:

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.)

Just short info for future readers:

In addition to the four built-in scopes, CDI also supports two pseudo-scopes:

  1. @Singleton
  2. @Dependent

and both above pseudo-scopes have interesting feature: CDI doesn't create a proxy object for them.

So all classes that are not proxyable (e.g. due to being final or due to lack of no-arg public constructor) can be marked with @Singleton or @Dependent - of course if it makes sense.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!