1 Reply Latest reply on Feb 27, 2018 2:22 AM by jaikiran

    Optional @Resource Elements

    walkerca

      Hi,

       

      I have a naming binding value setup in my domain.xml.  The parameter has a suitable default and I want the app to load if it is missing.  If the deployment or the developer has not added this item to their config, I'd still like the app to come up.

       

      I'm using a JNDI lookup in a Singleton/Startup/PostConstruct method.  Everything's working.  However, it seems like this could be better if there were an optional flag on the Resource annotation. 

       

      Guice and Spring have this concept.  Does anyone know if JavaEE WildFly does in 10 or a future version?

       

      Thanks,

      Carl

        • 1. Re: Optional @Resource Elements
          jaikiran

          A @Resource injection is a hard dependency and there's no construct to make it optional in Java EE.

           

          You could probably do away with the injection and instead do a manual JNDI Context.lookup(...) yourself in the lifecycle startup method (like a @PostConstruct) of the component and catch the NameNotFoundException and ignore it, to make the resource "optional" within your application.