这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface Default {
public static final class Literal extends AnnotationLiteral<Default> implements Default {
public static final Literal INSTANCE = new Literal();
private static final long serialVersionUID = 1L;
}
}
@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface MyQualifier {
@Nonbinding String value();
}
package com.bolingcavalry.service;
public interface HelloQualifier {
String hello();
}
package com.bolingcavalry.service.impl;
import com.bolingcavalry.service.HelloQualifier;
import javax.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class HelloQualifierA implements HelloQualifier {
@Override
public String hello() {
return this.getClass().getSimpleName();
}
}
package com.bolingcavalry;
import com.bolingcavalry.service.HelloQualifier;
import com.bolingcavalry.service.impl.HelloQualifierA;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import javax.inject.Inject;
@QuarkusTest
public class QualifierTest {
@Inject
HelloQualifier helloQualifier;
@Test
public void testQualifier() {
Assertions.assertEquals(HelloQualifierA.class.getSimpleName(),
helloQualifier.hello());
}
}
package com.bolingcavalry.annonation;
import javax.enterprise.util.Nonbinding;
import javax.inject.Qualifier;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface MyQualifier {
@Nonbinding String value();
}
public interface HelloPriority {
String hello();
}
@ApplicationScoped
@Alternative
@Priority(1001)
public class HelloPriorityA implements HelloPriority {
@Override
public String hello() {
return this.getClass().getSimpleName();
}
}
@ApplicationScoped
@Alternative
@Priority(1002)
public class HelloPriorityB implements HelloPriority {
@Override
public String hello() {
return this.getClass().getSimpleName();
}
}
@ApplicationScoped
@Alternative
@Priority(1003)
public class HelloPriorityC implements HelloPriority {
@Override
public String hello() {
return this.getClass().getSimpleName();
}
}
@QuarkusTest
public class PriorityTest {
@Inject
HelloPriority helloPriority;
@Test
public void testSelectHelloInstanceA() {
Assertions.assertEquals(HelloPriorityC.class.getSimpleName(),
helloPriority.hello());
}
}
package com.bolingcavalry.service;
public interface HelloInstance {
String hello();
}
package com.bolingcavalry.service.impl;
import com.bolingcavalry.service.HelloInstance;
import javax.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class HelloInstanceA implements HelloInstance {
@Override
public String hello() {
return this.getClass().getSimpleName();
}
}
package com.bolingcavalry;
import com.bolingcavalry.service.HelloInstance;
import com.bolingcavalry.service.impl.HelloInstanceA;
import com.bolingcavalry.service.impl.HelloInstanceB;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
@QuarkusTest
public class InstanceTest {
@Inject
Instance<HelloInstance> instance;
@Test
public void testSelectHelloInstanceA() {
Class<HelloInstanceA> clazz = HelloInstanceA.class;
Assertions.assertEquals(clazz.getSimpleName(),
instance.select(clazz).get().hello());
}
@Test
public void testSelectHelloInstanceB() {
Class<HelloInstanceB> clazz = HelloInstanceB.class;
Assertions.assertEquals(clazz.getSimpleName(),
instance.select(clazz).get().hello());
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://xinchen.blog.csdn.net/article/details/123939148
内容来源于网络,如有侵权,请联系作者删除!