public class MyCustomPropertyFileConfig implements PropertyFileConfig { @Override public String getPropertyFileName() { return "myconfig.properties"; } }看文档中描述应该还有更复杂的方式.不做研究,反正也用不到.
@ApplicationScoped public class SomeRandomService { @Inject @ConfigProperty(name = "endpoint.poll.interval", defaultValue = "11") private Integer pollInterval; @Inject @ConfigProperty(name = "endpoint.poll.servername", defaultValue = "www.baidu.com") private String pollUrl; ... }
在实际的使用为,首先配置jboss配置文件中的系统参数.然后配置META-INF/apache-deltaspike.properties的参数.最后配置注入点的defaultValue.避免空值.
其将按顺序进行读取.<system-properties> <property name="endpoint.poll.interval" value="15"/> </system-properties>
MyBean myBean = BeanProvider.getContextualReference(MyBean.class, false);
如果你寻找给定的接口和一个实现,第二个参数传递True
解决一个可选的上下文实例:
MyServiceInterface optionalService = BeanProvider.getContextualReference(MyServiceInterface.class, true);
也可以通过cdi 注解的方式获取对应的实例:
Literal Implementation for '@MyQualifier'
import javax.enterprise.util.AnnotationLiteral; //... public class MyQualifierLiteral extends AnnotationLiteral<MyQualifier> implements MyQualifier { }
更多的用法:
//以下示例将返回一个与”@myqualifier相关的上下文实例。 MyBean myBean = BeanProvider.getContextualReference(MyBean.class, false, new MyQualifierLiteral()); //也可以通过字符串名称来查找: Object myBean = BeanProvider.getContextualReference("myBean", false); //通过字符串名称来查找,并且有预期的结果类型: MyBean myBean = BeanProvider.getContextualReference("myBean", false, MyBean.class); //解决一个给定类型的所有实例的上下文,如接口的所有实现 List<MyServiceInterface> myServiceList = BeanProvider.getContextualReferences(MyServiceInterface.class, false);
Since dependent scoped beans have a special role in CDI (you have to destroy them manually - especially if you get them via a manual lookup), you can also call the previous util method with an additional parameter to filter dependent scoped instances.
Resolving All Contextual Instances of a Given Type without Dependent@Inject private BeanManager beanManager;
如果有特殊情况下无法使用@Inject,就可以使用下述代码获取BeanManager.
BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
可与cdi的替换注解@Alternative配合使用,更灵活.但我不会去用,感觉会搞的很复杂.
@Exclude public class NoBean { }ProjectStage为Development的情况下将不注册为cdi bean.
@Exclude(ifProjectStage = ProjectStage.Development.class) public class MyBean { }
ProjectStage如果不是Development,将不注册为cdi bean.
@Exclude(exceptIfProjectStage = ProjectStage.Development.class) public class MyDevBean { }
下面是@Exclude基于项目xml配置来判断是否注册
@Exclude(onExpression = "db==prodDB") public class DevDbBean { }
它还可以通过expressioninterpreter自定义表达式来判断
@Exclude(onExpression = "db eq prodDB", interpretedBy = SimpleExpressionInterpreter.class) public class DevDbBean { } public class SimpleExpressionInterpreter implements ExpressionInterpreter<String, Boolean> { @Override public Boolean evaluate(String expression) { if(expression.contains(" eq ")) { //... } //... } }
apache也提供了与@Alternative配合使用.
说明:ProjectStage如果不是Development,并且bean.xml中也进行了配置,那么注册使用.
@Exclude(exceptIfProjectStage = ProjectStage.Development.class) @Alternative public class MyDevBean { }
自定义expressioninterpreter
默认情况下,只有一个非常简单的和有限的语法是支持。在实际工程中通常有非常具体的要求。因为它会支持他们大多数是非常复杂的,它是为用户实现一个优化的语法更容易。对于这样的情况,一个自定义的expressioninterpreter是必要的:
@Alternative @Exclude(onExpression = "environment!=HSQL", interpretedBy = ConfigAwareExpressionInterpreter.class) public class DevDbBean implements DbBean { } public class ConfigAwareExpressionInterpreter implements ExpressionInterpreter<String, Boolean> { public Boolean evaluate(String expression) { if (expression == null) { return false; } String[] values = expression.split("!="); if (values.length != 2) { throw new IllegalArgumentException("'" + expression + "' is not a supported syntax"); } String configuredValue = ConfigResolver.getPropertyValue(values[0], null); //exclude if null or the configured value is different return configuredValue == null || !values[1].trim().equalsIgnoreCase(configuredValue); } }
public abstract class PayByQualifier extends AnnotationLiteral<PayBy> implements PayBy {} PayBy paybyCheque = new PayByQualifier() { public PaymentMethod value() { return CHEQUE; } };
DeltaSpike提供的Literals如下:
@Inject @InjectableResource("myfile.properties") private InputStream inputStream;这可以用来读取文件的路径,或在您的本地文件系统,使用两个默认的实现:classpathresourceprovider和fileresourceprovider。如果需要的话(例如数据库的LOB,NoSQL存储区),可以扩展injectableresourceprovider接口允许从其他来源的读取。
public class CustomClassDeactivator implements ClassDeactivator { private static final long serialVersionUID = 1L; @Override public Boolean isActivated(Class<? extends Deactivatable> targetClass) { if (targetClass.equals(SecurityExtension.class)) { return Boolean.FALSE; } return null; //no result for the given class } }Now, we can use the file /META-INF/apache-deltaspike.properties (or any other ConfigSource) with the following key/value:
org.apache.deltaspike.core.spi.activation.ClassDeactivator=org.test.CustomClassDeactivator