Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd use a dynamic producer, using a <code>Qualifier</code> to identify the desired environment</p> <pre><code>// The qualifier for the production/qa/unit test @Qualifier @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) public @interface Stage { String value() default "production"; } // The interface for the stage-dependant service public interface Greeting{ public String sayHello(); } // The production service @Stage("production") public class ProductionGreeting implements Greeting{ public String sayHello(){return "Hello customer"; } } // The QA service @Stage("qa") public class QAGreeting implements Greeting{ public String sayHello(){return "Hello tester"; } } // The common code wich uses the service @Stateless public class Salutation{ @Inject Greeting greeting; public String sayHello(){ return greeting.sayHello(); }; } // The dynamic producer public class GreetingFactory{ @Inject @Any Instance&lt;Greeting&gt; greetings; public String getEnvironment(){ return System.getProperty("deployenv"); } @Produces public Greeting getGreeting(){ Instance&lt;Greeting&gt; found=greetings.select( new StageQualifier(getEnvironment())); if (!found.isUnsatisfied() &amp;&amp; !found.isAmbiguous()){ return found.get(); } throw new RuntimeException("Error ..."); } public static class StageQualifier extends AnnotationLiteral&lt;Stage&gt; implements Stage { private String value; public StageQualifier(String value){ this.value=value; } public String value() { return value; } } } </code></pre> <p>So here the container injects all available <code>Greeting</code> implementations into <code>GreetingFactory</code>, which in turn serves as <code>@Producer</code> for the intended one, basing the decision on the system property 'deployenv'.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload