0

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!

Ciprian
  • 139
  • 1
  • 3
  • 12
  • 1
    You likely want to have a look at [CDI](http://www.cdi-spec.org/learn/). WildFly uses [Weld](http://weld.cdi-spec.org/) for it's implementation. – James R. Perkins Mar 27 '18 at 19:34
  • @James Thank you for your response! I am very sorry I was sure I already replied! Must have missed to hit the "Add Comment" button. About your hint: That is exactly what I am trying to use. I am already using filters, startup Hooks and Injection for my DAOs. However, I can't find a hint about how to achieve what I was trying to to in my upper example. Maybe Producer-Methods is what I am looking for. Anyway, thank you very much for the reply! :) – Ciprian Mar 29 '18 at 05:43

1 Answers1

0

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

Ciprian
  • 139
  • 1
  • 3
  • 12