Search code examples
javajakarta-eedependency-injectionprimitiveboxing

Inject resource as primitive type


I'm currently injecting a resource as boolean like this:

@Resource(name = "java:global/myBooleanValue")
String strValue;
boolean boolValue = Boolean.parseBoolean( strValue );

Is there a way to inject the resource directly as boolean using the type parameter? @Resource(name = ..., type = Boolean.class) doesn't seem to work.


Solution

  • You can do it like this:

    public class Test {
        private boolean boolValue;
    
        @Resource(name = "java:global/myBooleanValue")
        private void setBoolValue(String: strVal) {
             boolValue = Boolean.parseBoolean(strVal);
        }
    }