如果@Resource无法解决,Glassfish / Wildfly不会失败? [英] Glassfish/Wildfly not failing if @Resource cannot be resolved?

查看:196
本文介绍了如果@Resource无法解决,Glassfish / Wildfly不会失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在servlet中试验 @Resource ,看看我们是否可以轻松地使用它来注入配置参数(这是Glassfish 4.1.1中的servlet 3.0 webapp)。

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

我在 https://stackoverflow.com/a/45715196 / 53897 我看到 @Resource String userName 的行为在使用时为null,如果web.xml中的env条目是无法解析的话不存在,根据规范是错误的,但是WildFly的行为是相同的。

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"?

推荐答案

这对于简单的env条目是允许的:

This is allowed for simple env entries:

EE.5.4.1.3

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.

//由部署者配置的最大免税数量。

@Resource int maxExemptions = 4; //默认为4

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

这篇关于如果@Resource无法解决,Glassfish / Wildfly不会失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆