@Service@ConditionalOnProperty( value="logging.enabled",赖注 havingValue = "true", matchIfMissing = true)class LoggingService { // ...}
public interface TryLookupIfProperty { String hello();}
public class TryLookupIfPropertyAlpha implements TryLookupIfProperty { @Override public String hello() { return "from " + this.getClass().getSimpleName(); }}
public class TryLookupIfPropertyBeta implements TryLookupIfProperty { @Override public String hello() { return "from " + this.getClass().getSimpleName(); }}
package com.bolingcavalry.config;import com.bolingcavalry.service.TryLookupIfProperty;import com.bolingcavalry.service.impl.TryLookupIfPropertyAlpha;import com.bolingcavalry.service.impl.TryLookupIfPropertyBeta;import io.quarkus.arc.lookup.LookupIfProperty;import javax.enterprise.context.ApplicationScoped;public class SelectBeanConfiguration { @LookupIfProperty(name = "service.alpha.enabled", stringValue = "true") @ApplicationScoped public TryLookupIfProperty tryLookupIfPropertyAlpha() { return new TryLookupIfPropertyAlpha(); } @LookupIfProperty(name = "service.beta.enabled", stringValue = "true") @ApplicationScoped public TryLookupIfProperty tryLookupIfPropertyBeta() { return new TryLookupIfPropertyBeta(); }}
package com.bolingcavalry;import com.bolingcavalry.service.TryLookupIfProperty;import com.bolingcavalry.service.impl.TryLookupIfPropertyAlpha;import io.quarkus.test.junit.QuarkusTest;import org.junit.jupiter.api.Assertions;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.Test;import javax.enterprise.inject.Instance;import javax.inject.Inject;@QuarkusTestpublic class BeanInstanceSwitchTest { @BeforeAll public static void setUp() { System.setProperty("service.alpha.enabled", "true"); } // 注意,前面的LookupIfProperty不能决定注入bean是否实力话,只能决定Instance.get是否能取到, //所以此处要注入的是Instance,而不是TryLookupIfProperty本身 @Inject Instance<TryLookupIfProperty> service; @Test public void testTryLookupIfProperty() { Assertions.assertEquals("from " + tryLookupIfPropertyAlpha.class.getSimpleName(), service.get().hello()); }}
public class SelectBeanConfiguration { @LookupIfProperty(name = "service.alpha.enabled", stringValue = "true") @ApplicationScoped public TryLookupIfProperty tryLookupIfPropertyAlpha() { return new TryLookupIfPropertyAlpha(); } @LookupUnlessProperty(name = "service.alpha.enabled", stringValue = "true") @ApplicationScoped public TryLookupIfProperty tryLookupIfPropertyBeta() { return new TryLookupIfPropertyBeta(); }}
@BeforeAllpublic static void setUp() { System.setProperty("service.alpha.enabled", "true");}
# LookupIfProperty,说的是be obtained by programmaticIndicates that a bean should only be obtained by programmatic lookup if the property matches the provided value.# IfBuildProfile,说的是be enabledthe bean will only be enabled if the Quarkus build time profile matches the specified annotation value.
public interface TryIfBuildProfile { String hello();}
public class TryIfBuildProfileProd implements TryIfBuildProfile { @Override public String hello() { return "from " + this.getClass().getSimpleName(); }}
public class TryIfBuildProfileDefault implements TryIfBuildProfile { @Override public String hello() { return "from " + this.getClass().getSimpleName(); }}
@Produces@IfBuildProfile("test")public TryIfBuildProfile tryIfBuildProfileProd() { return new TryIfBuildProfileProd();}@Produces@DefaultBeanpublic TryIfBuildProfile tryIfBuildProfileDefault() { return new TryIfBuildProfileDefault();}
@InjectTryIfBuildProfile tryIfBuildProfile;@Testpublic void testTryLookupIfProperty() { Assertions.assertEquals("from " + TryLookupIfPropertyAlpha.class.getSimpleName(), service.get().hello());}@Testpublic void tryIfBuildProfile() { Assertions.assertEquals("from " + TryIfBuildProfileProd.class.getSimpleName(), tryIfBuildProfile.hello());}
# LookupIfProperty的描述,如果属性匹配,则此bean可以被获取使用Indicates that a bean should only be obtained by programmatic lookup if the property matches the provided value.# IfBuildProperty的描述,如果构建属性匹配,则此bean是enabledthe bean will only be enabled if the Quarkus build time property matches the provided value
@Dependentpublic class TracerConfiguration { @Produces @IfBuildProperty(name = "some.tracer.enabled", stringValue = "true") public Tracer realTracer(Reporter reporter, Configuration configuration) { return new RealTracer(reporter, configuration); } @Produces @DefaultBean public Tracer noopTracer() { return new NoopTracer(); }}
(责任编辑:综合)
上市公司一季度业绩预告已达178家 低估值高增长细分领域龙头股受关注
埃斯顿(002747.SZ):埃斯顿投资减持749.18万股 占公司总股本的比例约为0.89%
城发环境吸收合并启迪环境方案获河南省财政厅及河南省国资委批复
深高速(00548.HK)年度净利润减少19.88% 末期现金股息每股0.43元