这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
@ApplicationScoped
public class ClassAnnotationBean {
public String hello() {
return "from " + this.getClass().getSimpleName();
}
}
@ApplicationScoped
public class ClassAnnotationBean {
public String hello() {
return "from " + this.getClass().getSimpleName();
}
}
@Path("/classannotataionbean")
public class ClassAnnotationController {
@Inject
ClassAnnotationBean classAnnotationBean;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String get() {
return String.format("Hello RESTEasy, %s, %s",
LocalDateTime.now(),
classAnnotationBean.hello());
}
}
时间点 | 常规作用域 | 伪作用域 |
---|---|---|
注入的时候 | 注入的是一个代理类,此时ClassAnnotationBean并未实例化 | 触发ClassAnnotationBean实例化 |
get方法首次执行的时候 | 1. 触发ClassAnnotationBean实例化2. 执行常规业务代码 | 1. 执行常规业务代码 |
package com.bolingcavalry.service.impl;
import io.quarkus.logging.Log;
import javax.enterprise.context.RequestScoped;
@RequestScoped
public class RequestScopeBean {
/**
* 在构造方法中打印日志,通过日志出现次数对应着实例化次数
*/
public RequestScopeBean() {
Log.info("Instance of " + this.getClass().getSimpleName());
}
public String hello() {
return "from " + this.getClass().getSimpleName();
}
}
package com.bolingcavalry;
import com.bolingcavalry.service.impl.RequestScopeBean;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.time.LocalDateTime;
@Path("/requestscope")
public class RequestScopeController {
@Inject
RequestScopeBean requestScopeBean;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String get() {
return String.format("Hello RESTEasy, %s, %s",
LocalDateTime.now(),
requestScopeBean.hello());
}
}
package com.bolingcavalry;
import com.bolingcavalry.service.impl.RequestScopeBean;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.containsString;
@QuarkusTest
class RequestScopeControllerTest {
@RepeatedTest(10)
public void testGetEndpoint() {
given()
.when().get("/requestscope")
.then()
.statusCode(200)
// 检查body内容,是否含有ClassAnnotationBean.hello方法返回的字符串
.body(containsString("from " + RequestScopeBean.class.getSimpleName()));
}
}
@Dependent
public class HelloDependent {
public HelloDependent(InjectionPoint injectionPoint) {
Log.info("injecting from bean "+ injectionPoint.getMember().getDeclaringClass());
}
public String hello() {
return this.getClass().getSimpleName();
}
}
@ApplicationScoped
public class DependentClientA {
@Inject
HelloDependent hello;
public String doHello() {
return hello.hello();
}
}
@QuarkusTest
public class DependentTest {
@Inject
DependentClientA dependentClientA;
@Inject
DependentClientB dependentClientB;
@Test
public void testSelectHelloInstanceA() {
Class<HelloDependent> clazz = HelloDependent.class;
Assertions.assertEquals(clazz.getSimpleName(), dependentClientA.doHello());
Assertions.assertEquals(clazz.getSimpleName(), dependentClientB.doHello());
}
}