Wildfly Injection 取决于条件 (system-属性)
Wildfly Injection depending on Condition (system-property)
我正在使用 Wildfly atm,我正在尝试进入@Annotations。
对于某些接口有多个实现,我希望我的应用程序根据某些条件选择某个实现(取决于系统-属性,在我的 standalone.xml 中指定)。
你能给我一些提示来阅读如何实现这种行为吗?
我不想直接将实现指定为 @Alternative,因为我想将各种注入与某些系统 属性 设置捆绑在一起。
standalone.xml
<system-properties>
<property name="env" value="stage"/>
</system-properties>
界面
public interface LoginServerDao {
public String test();
}
实施 1
@MyCustomConditionalAnnotation(env = "live")
public class LoginServerDaoImpl implements LoginServerDao {
@Override
public String test() {
return "live";
}
}
实施 2
@MyCustomConditionalAnnotation(env = "stage")
public class DummyLoginServerDaoImpl implements LoginServerDao {
@Override
public String test() {
return "dummy";
}
}
注释界面
@???
public @interface MyCustomConditionalAnnotation {
String env() default "test";
???
}
我真的很感激任何对此的帮助(或者提到一些完全不同的 solution/pattern 我可能甚至没有想过)。
提前致谢!
我终于找到了解决问题的方法!
Stereotypes
在 Whosebug 上找到答案:
@Cassio Mazzochi Molin explained under Writing your own alternative stereotype
我正在使用 Wildfly atm,我正在尝试进入@Annotations。 对于某些接口有多个实现,我希望我的应用程序根据某些条件选择某个实现(取决于系统-属性,在我的 standalone.xml 中指定)。
你能给我一些提示来阅读如何实现这种行为吗?
我不想直接将实现指定为 @Alternative,因为我想将各种注入与某些系统 属性 设置捆绑在一起。
standalone.xml
<system-properties>
<property name="env" value="stage"/>
</system-properties>
界面
public interface LoginServerDao {
public String test();
}
实施 1
@MyCustomConditionalAnnotation(env = "live")
public class LoginServerDaoImpl implements LoginServerDao {
@Override
public String test() {
return "live";
}
}
实施 2
@MyCustomConditionalAnnotation(env = "stage")
public class DummyLoginServerDaoImpl implements LoginServerDao {
@Override
public String test() {
return "dummy";
}
}
注释界面
@???
public @interface MyCustomConditionalAnnotation {
String env() default "test";
???
}
我真的很感激任何对此的帮助(或者提到一些完全不同的 solution/pattern 我可能甚至没有想过)。
提前致谢!
我终于找到了解决问题的方法!
Stereotypes
在 Whosebug 上找到答案:
@Cassio Mazzochi Molin explained under Writing your own alternative stereotype