Glassfish/Wildfly not failing if @Resource cannot be resolved?

吃可爱长大的小学妹 提交于 2020-01-15 04:50:12

问题


I am experimenting with @Resource in a servlet to see if we can use it to inject configuration parameters easily (this is servlet 3.0 webapp in Glassfish 4.1.1).

I was told in https://stackoverflow.com/a/45715196/53897 that the behavior I saw of a @Resource String userName being null when used if it could not be resolved if the env-entries in web.xml were not present, to be wrong according to the specification, but that the behavior of WildFly was the same.

Question is: Is this a known bug? Is there a flag that can enforce the "fail if not resolvable"?


回答1:


This is allowed for simple env entries:

EE.5.4.1.3

....
It’s often convenient to declare a field or method as an injection target, but specify a default value in the code, as illustrated in the following example.

// The maximum number of tax exemptions, configured by the Deployer.
@Resource int maxExemptions = 4; // defaults to 4

To support this case, the container must only inject a value for this resource if the deployer has specified a value to override the default value.
.....



来源:https://stackoverflow.com/questions/45716813/glassfish-wildfly-not-failing-if-resource-cannot-be-resolved

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