问题
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