io.quarkus.arc.lookup.LookupIfProperty Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-arc Show documentation
Show all versions of quarkus-arc Show documentation
Build time CDI dependency injection
package io.quarkus.arc.lookup;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import jakarta.enterprise.inject.Instance;
/**
* Indicates that a bean should only be obtained by programmatic lookup if the property matches the provided value.
*
* This annotation is repeatable. A bean will be included if all the conditions defined by the {@link LookupIfProperty} and
* {@link LookupUnlessProperty} annotations are satisfied.
*
*
*
* interface Service {
* String name();
* }
*
* {@literal @LookupIfProperty(name = "service.foo.enabled", stringValue = "true")}
* {@literal @ApplicationScoped}
* class ServiceFoo implements Service {
*
* public String name() {
* return "foo";
* }
* }
*
* {@literal @ApplicationScoped}
* class ServiceBar implements Service {
*
* public String name() {
* return "bar";
* }
* }
*
* {@literal @ApplicationScoped}
* class Client {
*
* {@literal @Inject}
* Instance<Service> service;
*
* void printServiceName() {
* // This would print "bar" if the property of name "service.foo.enabled" was set to false
* // Note that service.get() would normally result in AmbiguousResolutionException
* System.out.println(service.get().name());
* }
* }
*
*
*
* @see Instance
*/
@Repeatable(LookupIfProperty.List.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE, ElementType.FIELD })
public @interface LookupIfProperty {
/**
* Name of the runtime property to check
*/
String name();
/**
* Expected {@code String} value of the runtime property (specified by {@code name}) if the bean should be looked up at
* runtime.
*/
String stringValue();
/**
* Determines if the bean is to be looked up when the property name specified by {@code name} has not been specified at all
*/
boolean lookupIfMissing() default false;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE, ElementType.FIELD })
@interface List {
LookupIfProperty[] value();
}
}