Search code examples
dependency-injectionannotationswildflysystem-properties

Wildfly Injection depending on Condition (system-property)


I am working with Wildfly atm and I am trying to get into @Annotations. Having several implementations for some interfaces I want my application to pick a certain implementation depending on some condition (depending on a system-property, specified in my standalone.xml).

Can you point me to some hints to read up how to achieve such behaviour.

I don't want to directly specify the implementation as an @Alternative, since i want to bundle various injections with some system-property settings.

standalone.xml

<system-properties>
    <property name="env" value="stage"/>
</system-properties>

Interface

public interface LoginServerDao {
    public String test();
}

Implementation 1

@MyCustomConditionalAnnotation(env = "live")
public class LoginServerDaoImpl implements LoginServerDao  {
    @Override
    public String test() {
        return "live";
    }
}

Implementation 2

@MyCustomConditionalAnnotation(env = "stage")
public class DummyLoginServerDaoImpl implements LoginServerDao  {
    @Override
    public String test() {
        return "dummy";
    }
}

Annotation Interface

@???
public @interface MyCustomConditionalAnnotation {

    String env() default "test";

    ???

}

I really appreciate any help with this (or refering to some completely different solution/pattern I might not even have thought about).

Thanks in advance!


Solution

  • I finally found the solution to my problem!

    Stereotypes

    Found the answer on stackoverflow here: Disable @Alternative classes

    @Cassio Mazzochi Molin explained under Writing your own alternative stereotype